Save Page as PDF

I would like to have a button on a page that says “Download” that would convert the current page’s HTML/CSS to a PDF and download it.

I’ve seen some JS libraries, but they don’t work great in my experience.

I’m sure there are some command-line options, but I’m not sure what LiveWhale might have available.

Has anyone ever done something like this?

In my personal case, I’ve used two manual approaches for something like this:

  1. Print the page and “Save as a PDF”, relying on the print styles to render the page out in an appropriate layout. (simplified, accessible)
  2. Use the browser’s developer tools to take a “full size screenshot”, capturing the entire page. (full styles, inaccessible)

Examples for this webpage: “Why let anything trouble ‘you’?” • Modern Languages & Literatures • Beloit College

  1. The saved PDF from the print operation. (Seems I need to look at print styles again.)
  2. The saved full size image.

As noted, neither offers both “full fidelity” and “accessible”. The print approach seems the closest to your likely need, if you’re willing to not have the page be 1:1 identical visually.

I think a page button can open the print dialog, but I don’t believe you can force it to use the “Save as PDF” option. Maybe a modal pop-up first that explains the steps, then when closed brings up the print dialog?

Otherwise, no clue on alternative solutions. I would be curious to know if there is anything.

Thanks,
Nick

I know I can print a page to PDF. I don’t count on users’ abilities to know that, though. I would love to have an easy button for visitors to click and download a PDF of the current page, but I suspect the solution is much more complex than I want to know.

To offer briefly: Yeah, we helped out with a “use an open-source library to generate PDFs from HTML pages” project in the past, but it ended up being a many-tendriled beast that took awhile to get working, never quite worked 100% how you’d hoped, and in the end did not make accessible PDFs. Adding to that, I didn’t get the sense from the library’s maintainers that there was energy around the idea of revamping it to provide accessible PDFs. So, I agree, PDF generation is something we’ve also decided to steer clear of internally in the future.