# Can a macro know, if it is invoked from a PDF export?

**URL:** <https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732>\
**Category:** Help / Discuss\
**Tags:** macro\
**Created:** [December 26, 2023, 6:40pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732 "2023-12-26T18:40:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [December 26, 2023, 6:40pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/1 "2023-12-26T18:40:06Z")

</div>

Hi all …

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:

```auto
{{velocity output="false"}}
$xcontext.put('data-export': 'true')
{{/velocity}}

```

and then inside the macro, I tried to read it:

```auto
{{groovy}}
if (xcontext.get("data-export"))
  println("export")
else
  println("no export")
{{/groovy}}

```

But it seems, that the xcontext is not shared between the template script and the macro script.

Are there any alternative approaches?

Thanks in advance

Holger

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [December 27, 2023, 7:30am UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/2 "2023-12-27T07:30:41Z")

</div>

Hi, the xcontext is shared for the same HTTP request.

For saving data, see [https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/SavingData/](https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/SavingData/)

Also note that you should be able to use the `export` action, and check the query string for the PDF export, as in:

```auto
#if ($xcontext.action == 'export' && $request.format == 'pdf')
...

```

See [https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/Exports#HAdvanced-2](https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/Exports#HAdvanced-2)

---

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [December 27, 2023, 8:54am UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/3 "2023-12-27T08:54:58Z")

</div>

Thanks for your help!

For whatever reason, the metadata script seems not to be executed in the same context as the macros!?

However, `xcontext.action == "export" && request.format == "pdf"` does the job 😊

Greets Holger

---

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [January 26, 2024, 4:11pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/4 "2024-01-26T16:11:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [January 29, 2024, 8:25am UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/5 "2024-01-29T08:25:49Z")

</div>

> [@hengels](#):
>
> The ExecutionContext is actually not shared between scripts that are called from the template (header, footer, metadata) and the exported page.

Yes, because the export is done in two steps:

- 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.

Let me know if this helps,  
Marius

---

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [January 30, 2024, 4:55pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/6 "2024-01-30T16:55:04Z")

</div>

Thanks for your answer. This is exactly, what I thought, should work. I put this script in the metadata field:

```auto
{{groovy}}
import org.xwiki.context.*
def context = services.component.getInstance(Execution.class).getContext()
context.setProperty("printing", "true")
{{/groovy}}

```

Then I try to read the property inside the page content like so:

```auto
{{groovy}}
import org.xwiki.context.*
def context = services.component.getInstance(Execution.class).getContext()
def printing = context.getProperty("printing")
println("lala " + printing);
{{/groovy}}

```

It simply renders “lala null”, regardless if the pages is rendered in view mode or if it is rendered for printing.

Regards Holger

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [January 31, 2024, 1:19pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/7 "2024-01-31T13:19:29Z")

</div>

> [@hengels](#):
>
> I put this script in the metadata field:
> 
> ```auto
> {{groovy}}
> import org.xwiki.context.*
> def context = services.component.getInstance(Execution.class).getContext()
> context.setProperty("printing", "true")
> {{/groovy}}
> 
> ```

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()`.

Let me know if this works.

Thanks,  
Marius

---

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [January 31, 2024, 2:33pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/8 "2024-01-31T14:33:59Z")

</div>

Hi Marius,

I didn’t know, that properties have to be declared 🙈 … if I understand it correctly, the following should work, right?

```auto
{{groovy}}
import org.xwiki.context.*
def context = services.component.getInstance(Execution.class).getContext()
context.newProperty("printing").type(java.lang.Boolean).initial(true).inherited().declare()
{{/groovy}}

```

Well, it doesn’t 🤔

Is it possible that the contexts are siblings? Or maybe the context executing the metadata is popped right after?

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [January 31, 2024, 3:34pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/9 "2024-01-31T15:34:29Z")

</div>

> [@hengels](#):
>
> Or maybe the context executing the metadata is popped right after?

That may be the case, but I have to debug to confirm. Next try is to use the `XWikiContext` instead. Can you try using `xcontext.put("printing", true)` ?

---

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [February 2, 2024, 8:42am UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/10 "2024-02-02T08:42:15Z")

</div>

Hi Marius,

please excuse my delayed response. I tried that, too. It does not work :-/

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [February 2, 2024, 1:55pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/11 "2024-02-02T13:55:03Z")

</div>

> [@mflorea](#):
>
> 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.

I was wrong actually… the metadata field is rendered **after** the document is rendered. The right solution that worked for me is this:

```auto
{{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}}

```

Hope this helps,  
Marius

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [February 2, 2024, 3:51pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/12 "2024-02-02T15:51:06Z")

</div>

@mflorea Would be interesting to document on the client-side PDF export page on exo, in a developer section 🙂

---

<div class="post-metadata">

**Author:** ![hengels](https://forum.xwiki.org/user_avatar/forum.xwiki.org/hengels/32/6445_2.png) [@hengels](https://forum.xwiki.org/u/hengels)\
**Post date:** [February 2, 2024, 7:29pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/13 "2024-02-02T19:29:36Z")

</div>

That works 🙂

Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [February 7, 2024, 9:59am UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/14 "2024-02-07T09:59:45Z")

</div>

I added some documentation here [https://extensions.xwiki.org/xwiki/bin/view/Extension/PDF%20Export%20Application/#HAdaptthecontentforPDFexport](https://extensions.xwiki.org/xwiki/bin/view/Extension/PDF%20Export%20Application/#HAdaptthecontentforPDFexport) .

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [February 7, 2024, 1:12pm UTC](https://forum.xwiki.org/t/can-a-macro-know-if-it-is-invoked-from-a-pdf-export/13732/15 "2024-02-07T13:12:59Z")

</div>

This is great (and a great content)! thanks
