Skip to main content

#Download Pdf0 discussing

Hi Guys,

Can you please suggest how can we print pdf from lwc component?

I tried window.print() which gives whole page, we only want contents from our lwc component. 

 

#LWC Development

#Download Pdf

6 answers
  1. Jul 17, 2024, 12:38 PM

    Hello community! 

    I had similar task and was able to complete it using just lwc! 

     

    For that I had to create the following js method:

     

    handlePrintClick () {

    let printableElement = this.template.querySelector('.print-section');

    if (printableElement) {

    let clone = printableElement.cloneNode(true);

    let printWindow = window.open('', '_blank');

    if (printWindow) {

    printWindow.document.body.appendChild(clone);

    let style = printWindow.document.createElement('style');

    style.textContent = `

    @page {

    size: auto; /* auto is the initial value */

    margin: 0mm; /* this affects the margin in the printer settings */

    }

    @media print {

    body {

    margin: 0;

    padding: 0;

    }

    .no-print {

    display: none;

    }

    }

    `;

    printWindow.document.head.appendChild(style);

    printWindow.focus();

    printWindow.print();

    printWindow.close();

    } else {

    console.error('Failed to open print window');

    }

    } else {

    console.error('Printable element not found');

    }

    }

    and you need to update the content of html with css class to the place which you wanted to print (I had lots of inner data, parent and childs)

     

    <div class="print-section">

    Data which should be printed

    </div>

    Note: In my case I had all css values in the same html file (not a separate css, I used style tag) 

0/9000

Static resources breaks in Visual Force Page rendered as PDF after changing a file inside the zip and re-uploading

 

I downloaded a working Static Resource used by a VF page and change an image, compressed the folder and re-upload again and ended-up with a not working VF page. None of the images were loading and the css not being applied.

 

I compared with other static resources and noticed a difference in the MIME type of file uploaded file.

The type of my re-uploaded zip file shows as "application/zip" vs the others showing as "application/x-zip-compressed". After many, but many posts reading, I realised this is not the root of the problem

 

Solution: When compressing the file, the individual folders inside the zip needs to be selected and compressed; whereas compressing the whole folder adds an extra folder to the path, breaking the link to the static resource from the VF page, eg.:

  • "myStaticResources.zip/myStaticResources/styles/style.css" instead of
  • "myStaticResources.zip/styles/style.css"

So the formula in the VF page: "{!URLFOR($Resource.myStaticResources, '/styles/style.css')}" won't work

 

Static resources breaks in Visual Force Page rendered as PDF after changing a file inside the zip and re-uploading I downloaded a working Static Resource used by a VF page and change an image, compres

 

Screenshot 2024-04-03 at 08.38.59.png

 

#VfPage  #Host Static Resource  #Upload Files  #Download Pdf  #Visualforce  #VisualForce Page  #Zip File

1 comment
  1. Jul 5, 2025, 7:31 PM

    OMG.  THANK YOU. 

    After hours of troubleshooting this, finally found your tip.  I was zipping up the folder also, and getting 404, pulling my hair out.  lol.  finally fixed.

0/9000

hi Ann - I was trying to learn from your post about creating a landing page to host a pdf (https://success.salesforce.com/answers?id=9063A000000Zm6LQAS ) and had a question

 

in the beginning of your code, you include

<h3 style="text-align: center;"> <a download="" href="http://go.propelPLM.com/custom-dev-steps" target="_blank" title="Download Whitepaper">CLICK TO DOWNLOAD</a></h3>

 

<h3><object data="http://storage.pardot.com/147881/3744/Four_Steps_to_Great_Custom_Development.pdf" height="900" type="application/pdf" width="100%">

<p style="text-align: center;">&nbsp;</p>

 

I wasn't sure what the first URL is for - I think you are using a button to let them download the paper, but it looked like the second URL was to that file

 

apologies, I am not very up to speed with HTML anymore - I haven't used it in many years and want to teach some of this to our team :)

1 comment
  1. Dec 19, 2018, 6:58 PM

    Jenna,

    It has been a while since I used this code. We aren't using Pardot any longer, so none of the links are going to work. I'm not sure on the code, since I don't remember what was on the page

    http://go.propelPLM.com/custom-dev-steps

    I know there was a link to download at the top directed to the PDF file itself. I thought I used an image file, since a PDF doesn't always render depending on browser/plug-ins.

    Sorry I couldn't be of more help. You might do a search on embedding PDF files, since I know that I "borrowed" the html, since I'm not very good at it either.

    Good luck!

    Ann Latham

    Marketing Director

    Propel <

    http://www.propelplm.com

    >

    408-806-0848

0/9000