Search code examples
phppdfstylesheetalternate

Generate PDF of page using alternate/print stylesheet


I want to generate a PDF of a webpage but apply an alternate, print-type stylesheet to it instead of the styles it uses now. Say, for example, I have a button on http://eorailway.co.uk to generate a PDF of the same page (which is run and administered by me, so therefore I can include any PHP/JS necessary to each page) but I want to apply alternate styling to it before generating the PDF.

At the moment I am using the dompdf PHP library to generate the PDF using the normal/default stylesheet, but cannot for the life of me think how to apply the alternate stylesheet to the page when clicking the "Generate PDF" button.

Any advice is most appreciated.


Solution

  • Since the site is under control, you could dynamically decide which stylesheets to include based on a query string parameter. i.e. http://example.com/page.php?stylesheet=print would have your template output only the alternate stylesheet, and your PDF library would fetch that page to generate.