I have a macro that should render structurally different for pdf exports.
I could write some flag somewhere in the metadata script of the pdf template, that is executed for every exported script. I tried to write to xcontext as follows:
Hi … me again on this topic … with a new requirement
Background: I have one template for a normal pdf with clickable links and another one for a printable pdf without links but with qr-codes. Some macro needs to know whether it shall render links or qr-codes.
The ExecutionContext is actually not shared between scripts that are called from the template (header, footer, metadata) and the exported page. How can the macro determine, from which template it is actually called?
the wiki pages included in the PDF export are rendered async in a background low-priority daemon thread (job) and the rendering results are collected
at the end, the PDF sheet is called (HTTP request) which:
renders the PDF template fields (cover, table of contents, header, footer)
aggregates the rendering results collected in the first step
This means that when the PDF template fields (cover, table of contents, header, footer) are evaluated, the wiki pages (including the macros) were already rendered (and in a different context: background thread vs. actual HTTP request).
There is an exception though: the metadata field from the template is rendered for each wiki page included in the export, before that page is rendered, and in the same execution context normally. So one option you have is to set some (execution) context flag from the metadata field of the template (using a script macro) and then to check that flag in the code of your macros.
Have you tried to mark the execution property you set as inheritable. The execution keeps a stack of execution contexts. During document rendering it’s often the case that an execution context is pushed on the stack and then popped. Properties that should be seen in the new contexts must be marked as inherited. So you need to check if the property exists, and if not then declare it, using the dedicated builder, calling inherited().
I was wrong actually… the metadata field is rendered after the document is rendered. The right solution that worked for me is this:
{{velocity}}
#set ($expectedTemplateReference = $services.model.resolveDocument('Some.CustomPDFTemplate'))
#set ($actualTemplateReference = $services.job.getCurrentJobStatus(['export', 'pdf']).request.template)
#if ($expectedTemplateReference.equals($actualTemplateReference))
Using my custom PDF template.
#else
Either no PDF export or using another PDF template.
#end
{{/velocity}}