Skip / avoid printing certain sections of a page

I’m sure this has come up before, but I can’t find anything about it, not even among the extensions.

I’ve edited a page where in a section I mention an attachment, which is linked from the page content. I now need to send a PDF export of this page and would like to hide the section that talks about the attachment (that plus mentions some reserved URL’s and hosts).

Is there anything that fits?

We used to be able to write the following with the old server-side PDF export:

Some content

{{velocity}}
#if ($request.action != 'export')
  Some content displayed when not exporting...
#end
{{/velocity}}

I don’t know if this still works (probably not since now each page to export is rendered in view mode I think).

cc @mflorea It would be nice to document how to do this now with the new client-side PDF export. Maybe it’s already documented?

Thanks

1 Like

I don’t have a ready-made solution but I think you could use regular CSS print styles to control the display of the content. Basically, you could add, e.g., a certain class attribute to all content that shouldn’t be printed and then centrally create a CSS style to hide it. If you want to make this easier to use for users, you could create a wiki macro that wraps its content in a group with the respective class like the following example macro code:

(% class="no-print" %)(((
{{wikimacrocontent/}}
)))

With a CSS rule like

@media print {
   .no-print {
       display: none;
   }
}

you could then prevent printing the macro content.

[Edit] Actually, you don’t even need to add any CSS, just use the class hidden-print that is already present in XWiki as it is defined in Bootstrap 3. So the following macro code in a wiki macro should do the trick:

(% class="hidden-print" %)(((
{{wikimacrocontent/}}
)))

Alternatively, you can of course also wrap any content you want to hide in such a group syntax (or add the class parameter directly on the element).

Thanks for the idea. Looks like it would do the job.

However, I think it’s a bit less clean than what we used to be able to write as the print or non-print content is produced even when not in that context. I hope we can still have some server-side IF.

I added some documentation on https://extensions.xwiki.org/xwiki/bin/view/Extension/PDF%20Export%20Application/#HAdaptthecontentforPDFexport .

@vmassol not all users are able to use the server-side IF (some don’t have script right and some don’t have coding skills). Moreover, using a script macro makes editing a pain: you need to edit the source. Besides that, the script is easily broken if someone without script right saves a change to the content.

The wiki rendering macro suggested by @MichaelHamann is much more user-friendly. Using the script directly makes sense only if the section you want to hide from PDF is very costly to generate. But for standard content the performance penalty is insignificant.

Thanks,
Marius