Go through this article : Convert HTML/CSS Content to a Sleek Multiple Page PDF File Using jsPDF JavaScript library. The provided script allows you to convert any HTML element to PDF. I modified the generate() function slightly so that it takes any HTML element id name and export it as PDF file:

generate = function(doc)
{
	var pdf = new jsPDF('p', 'pt', 'a4');
	pdf.setFontSize(18);
	pdf.fromHTML(document.getElementById(doc), 
		margins.left, // x coord
		margins.top,
		{
			// y coord
			width: margins.width// max width of content on PDF
		},function(dispose) {
			headerFooterFormatting(pdf, pdf.internal.getNumberOfPages());
		}, 
		margins);
		
	var iframe = document.createElement('iframe');
	iframe.setAttribute('style','position:absolute;right:0; top:0; bottom:0; height:100%; width:650px; padding:20px;');
	document.body.appendChild(iframe);
	
	iframe.src = pdf.output('datauristring');
};

It works 100% on Chrome but Im not sure about other browsers.

Answer from Zuhair on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 66609249 › displaying-and-downloading-a-pdf-in-an-iframe
javascript - Displaying and downloading a pdf in an iframe - Stack Overflow
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> blah <script src="https://github.com/foliojs/pdfkit/releases/download/v0.11.0/pdfkit.standalone.js"></script> <script src="https://github.com/devongovett/blob-stream/releases/download/v0.1.3/blob-stream.js"> </script> </head> <body> <script> const doc = new PDFDocument const stream = doc.pipe(blobStream()) doc.fontSize(25).text('Testing document', 100, 80); console.log(doc); doc.end(); stream.on('finish', function() { window.src = stream.toBlobURL('application/pdf'); const a = document.createElement('a'); a.hre
🌐
Wisej.NET
wisej.com › support › question › print-pdf-file-by-iframe
Javascript Print Pdf file by IFrame – Wisej.NET
May 15, 2020 - You can use jsfiddle to test your javascript code. See https://www.w3schools.com/jsref/event_onload.asp ... var objFra = document.createElement('iframe'); // Create an IFrame. objFra.style.visibility = "hidden"; // Hide the frame. objFra.src = "ReadMe_001.pdf"; // Set source not done .pdf. objFra.onload = function(){ objFra.contentWindow.focus(); // Set focus.
Discussions

javascript - download PDF file from an Iframe content with jsPDF - Stack Overflow
i want to save a PDF file which contains an IFrame Content. I use the jsPDF Javascript library for. Here is my Code as yet: function toPDF(){ var pdf = new jsPDF('p', 'in', 'lett... More on stackoverflow.com
🌐 stackoverflow.com
April 18, 2017
html - How to download a file from iframe in javascript - Stack Overflow
I have a website in which I am using my chat website as in iframe as follows, More on stackoverflow.com
🌐 stackoverflow.com
lightning web components - PDF viewer on iFrame not showing download and print button - Salesforce Stack Exchange
I am sorry, I am in IST hours, ... works as expected on desktop but doesn't work on salesforce 1 mobile app, could you please suggest similar workaround which will work on mobile phones? ... I dont have experience with mobile app since it will be much different than this approach. You can however do this, a dirty approach will be to Save the PDF to contentDocument (the one you are displaying to the users) - get the DownloadableUrl from ... More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
javascript - How do I save iframe content and download it as a PDF file? - Stack Overflow
My goal is to print the content of an iframe into a PDF file. My code is currently only able to save an image. Whenever I replace the img tag with an iframe tag, the iframe content doesn't show up More on stackoverflow.com
🌐 stackoverflow.com
October 7, 2019
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 58271834 › how-do-i-save-iframe-content-and-download-it-as-a-pdf-file
javascript - How do I save iframe content and download it as a PDF file? - Stack Overflow
October 7, 2019 - I think the problem is due to the iframe's source ... $('#downloadPDF').click(function () { domtoimage.toPng(document.getElementById('content2')) .then(function (blob) { var pdf = new jsPDF('l', 'pt', [$('#content2').width(), $('#content2').height()]); pdf.addImage(blob, 'PNG', 0, 0, $('#content2').width(), $('#content2').height()); pdf.save("test.pdf"); that.options.api.optionsChanged(); }); }); /* $('#cmd2').click(function() { var options = { //'width': 800, }; var pdf = new jsPDF('p', 'pt', 'a4'); pdf.addHTML($("#content2"), -1, 220, options, function() { pdf.save('admit_card.pdf'); }); }); */
Top answer
1 of 2
3

To print html from iframe, you need to get content from iframe. Source code to get content from iframe (I got from this):

function getFrameContents() {
    var iFrame = document.getElementById('frame');
    var iFrameBody;
    if (iFrame.contentDocument) { // FF
        iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
    } else if (iFrame.contentWindow) { // IE
        iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
    }
    //alert(iFrameBody.innerHTML);
    return iFrameBody.innerHTML
}

So you replace:

source = $('#frame')[0]

with

source = getFrameContents();

or simple with jQuery:

source = $("#frame").contents().find('body')[0];

However, there is still issue about CORS (Cross-Origin Resource Sharing). To solve it, you can create web server and put you html code and iframe src in there and see how it work.

2 of 2
2

Looks like an open case on Github for this very issue, still open since 2015. See https://github.com/MrRio/jsPDF/issues/496. Maybe there is no solution.

Plus, in the source code is the following notation before the jsPDFAPI.fromHTML function that you are calling. Please see the last point re tables which may affect your work.

* Converts HTML-formatted text into formatted PDF text.
*
* Notes:
* 2012-07-18
* Plugin relies on having browser, DOM around. The HTML is pushed into dom and traversed.
* Plugin relies on jQuery for CSS extraction.
* Targeting HTML output from Markdown templating, which is a very simple
* markup - div, span, em, strong, p. No br-based paragraph separation supported explicitly (but still may work.)
* Images, tables are NOT supported.

Also - I tried to set up a working snippet here and in jsfiddle but could not get jsPDF.fromHTML to work. I am not a jsPDF expert, but I've been around the block and I don't get the feeling that jsPDF is very robust - you may want to cast your net wider and find another plugin. Just my opinion.

🌐
PKP Community
forum.pkp.sfu.ca › software support
[OJS 3.1.1.4] How to embed a pdf file in an iFrame - Software Support - PKP Community Forum
May 19, 2020 - Hi, How do you embed a pdf file in an iFrame? Apparently you can’t use the same method you use when you insert images in your html file. If we use the same method it will start downloading the pdf when opening the artic…
🌐
Medium
medium.com › @diptimaya.patra › generate-pdf-in-background-using-html-file-in-a-hidden-iframe-in-react-app-222516205e67
Generate PDF in Background using HTML file in a Hidden iFrame in React App | by Diptimaya Patra | Medium
November 6, 2023 - Use HTML file (a template file with JavaScript methods to fill data) Should not show the file in the UI · Press enter or click to view image in full size · Photo by CURVD® on Unsplash · To Achieve the requirements, I used/planned the following things. jsPDF to generate HTML content as PDF · Use of iFrame to load HTML file ·
🌐
Stack Overflow
stackoverflow.com › questions › 67632252 › how-to-save-iframe-as-pdf-or-image
html - How to save iframe as PDF or Image - Stack Overflow
<html> <head> <script src="jspdf.umd.min.js"></script> <script src="html2canvas.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.8.1/html2pdf.bundle.min.js" ></script> <script > function createPdf(){ window.jsPDF = window.jspdf.jsPDF var pdf = new jsPDF() var element = document.getElementById("capture") pdf.save("output.pdf") } </script> </head> <body> <iframe id="capture" src="https://wikipedia.org/wiki/Main_Page" style=" width: 100%; height: 600px" frameborder="0"></iframe> <button onclick="createPdf()">create pdf </button> </body> </html>
🌐
WebDeveloper.com
webdeveloper.com › community › 250344-iframe-to-pdf
IFrame to PDF
Thanks a lot. R. ... Yes, you can use a PDF virtual printer driver. By highlighting the specific text you want to convert, you can click FILE->PRINT and choose to print only the selected text.
Top answer
1 of 3
1
  • I don't understand why there's brackets around an attribute: [src] If you don't already know: Don't do that.

  • <iframe> doesn't have type as a valid attribute, but type does work for <iframe>'s sister tags <object> and <embed>.

The following Demo does not function on SO due to their restrictive sandbox. Go to this Plunker to see a functional Demo. Source PDF courtesy of PDFObject


Plunker --- Demo ====

It looks like Plunker no longer runs embeded content anymore, so if you want to review a functioning demo, simply copy and paste the entire code in any text editor (Notepad, Notepad++, etc.) and save as an HTML file (.html file extension).


<!DOCTYPE html>
<html>

<head>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    figure {
      display: table;
      border: 3px ridge grey;
      position: relative;
      width: 96vw;
      min-height: 250px;
      height: auto;
      margin: 0 auto 35px;
    }
    
    figcaption {
      border: 3px ridge grey;
      text-align: center;
      font: 900 20px/1.5 Verdana;
      padding: 0 0 8px 0;
      background: rgba(51, 51, 51, 0.3);
      color: #fff;
    }
    
    iframe,
    object,
    embed {
      overflow: hidden;
      position: absolute;
    }
  </style>
</head>

<body>

  <figure>
    <figcaption>IFRAME</figcaption>
    <iframe src="https://pdfobject.com/pdf/sample-3pp.pdf#page=1" class="frameSet" frameborder="0" allowfullscreen width="100%" height="100%"></iframe>
  </figure>

  <figure>
    <figcaption>OBJECT</figcaption>
    <object data="https://pdfobject.com/pdf/sample-3pp.pdf#page=2" class="objectSet" type="application/pdf" width="100%" height="100%"></object>
  </figure>

  <figure>
    <figcaption>EMBED</figcaption>
    <embed src="https://pdfobject.com/pdf/sample-3pp.pdf#page=3" class="embedSet" type="application/pdf" width="100%" height="100%">
  </figure>

</body>


</html>

2 of 3
1

You need to modify your URL like the below

iframeURL =  'urlThatYouGetFromAPI' + '&embedded=true'
<iframe [src]="iframeURL"></iframe>

Actually, All you need is adding this string &embedded=true to the end of URL.

Also you can use domSanatizer to make it more safe

 readonly #domSanitizer = inject(DomSanitizer);
 iframeURL = this.#domSanitizer.bypassSecurityTrustResourceUrl(
                `${URL}&embedded=true`
            );
🌐
Highcharts
highcharts.com › board index › highslide js - the javascript image and media viewer › highslide js usage
Iframe : generate pdf - Highcharts official support forum
Hi I have seen one post here to add the lines to print the iframe content and it's excellent, is it possible to generate a pdf of the content of the iframe ? or something similar to have a button to download a pdf of the content iframe Thanks
🌐
Reddit
reddit.com › r/javascript › need a way to save the contents of an iframe
r/javascript on Reddit: Need a way to save the contents of an iframe
March 2, 2015 -

Hello, and thanks for taking the time.

I am working on a project where we have a pdf appear in an iframe. I have two buttons on the page, one for printing the pdf, and one for saving the pdf. I was able to print the iframe contents with:

document.getElementById("pdf").contentWindow.print();

However I can't find a way to just save the contentwindow.. Does anyone know if this is possible?

Thanks!

🌐
EncodedNA
encodedna.com › javascript › how-to-print-a-pdf-document-using-javascript.htm
How to Print a PDF Document using JavaScript
March 7, 2026 - Last Updated: 7th March 2026 Usually, we download a PDF file on our computer, open the file and click the print button to print its contents. However, you can print a PDF document directly from your web page using JavaScript. You can load the PDF into an iframe (or similar container) and trigger ...
🌐
SitePoint
sitepoint.com › blog › javascript › load pdf into iframe and call print
Load PDF into iframe and call print — SitePoint
February 12, 2024 - Loading a PDF into an iFrame using JavaScript is quite straightforward. You can use the ‘src’ attribute of the iFrame to set the source to the PDF file.