1

How can one export a LWC as a PDF?

(Same as this post, with a slightly different angle)

A couple hours of research have not yielded any useful results

Requirement: Create a way to export the LWC so it can easily be shared outside of SF. A pdf would be ideal but it doesn't have to be, as long as it can be exported with just a few clicks and it maintains the formatting of the LWC (see sample image below).

enter image description here

The LWC may have any amount of Grant and/or Line Item records, so it could be half a page or 10 pages long. And to clarify, I do not mean create a PDF from the LWC, I mean create a PDF of the LWC itself. I have a LWC that creates a table with specific formatting that I need to export (pdf would be ideal, but not strictly necessary). It will be shared with higher ups and deals with budgets so it needs to be crisp and maintain the formatting of the LWC. I was considering/exploring:

  • Placing the LWC in a Visualforce (VF) page and render VF page as pdf (VF can't render as pdf JavaScript rendered content, such as a LWC)
  • Standard page print functionality (not ideal: omits colors, breaks table, includes link URLs)
  • Recreate the LWC table in report and export from there (intended formatting is lost, it would require recreating the formatting in excel sheet or something like that)
  • Custom app from AppExchange (none found that would fulfill this need)
  • Basic screenshot of the LWC (loses image quality, text not selectable, not scalable for when LWC has many rows)

If you have run into this before, found a solution, and could point me in the right direction, I would really appreciate it!

4
  • 1
    I've never found a 'good' solution. The only solution that sort of worked was to pass the data from the LWC to a hidden VF page via the Lightning Message Service, have the VF page build the printable page from the payload using javascript, and either use render as/click to print and have the user choose Print to PDF. But once the data goes over one page, you get into some painful issue in my experience Commented Feb 15 at 21:02
  • 1
    It may be possible today with html2pdf if you're running all your LWC in Light DOM. I haven't tried it out, though, so this is more of a musing than an answer.
    – sfdcfox
    Commented Feb 15 at 22:32
  • @sfdcfox thanks for the suggestion and the resources. I am just getting started with LWCs so sounds like I'd be diving right into the deep end. But I will explore that option. If you can think of any major caveats that a newer developer might run into with this, I would appreciate any tips. Currently my LWC is used in a Lightning Record Page as well as a record detail page in an Experience site. It's hard for me to fully comprehend what I'm reading in those articles, but as best as I can interpret them, I assume using the LWC in those two places would not be a problem. Commented Feb 16 at 16:33
  • @BritishBoyinDC, we'll most definitely have some that go over two pages so it may not be ideal. But at this point I am willing to consider anything haha. Thank you for the suggestion! Commented Feb 16 at 16:35

0

You must log in to answer this question.

Browse other questions tagged .