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 Overflowjavascript - download PDF file from an Iframe content with jsPDF - Stack Overflow
html - How to download a file from iframe in javascript - Stack Overflow
lightning web components - PDF viewer on iFrame not showing download and print button - Salesforce Stack Exchange
javascript - How do I save iframe content and download it as a PDF file? - Stack Overflow
Videos
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.
Try this solution.
<button id="generatePDF">Generate PDF</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$( '#generatePDF' ).click( function()
{
var pdf = new jsPDF('a4');
pdf.text("Some text inside PDF", 10, 10);
$( '#docpdf' ).attr('src', pdf.output('datauristring'));
});
</script>
<iframe id="docpdf" style="background-color:#EEE; height:400px;">
PDF goes here
</iframe>
The general approach I use for such situations is to start poking around in the Developer tools of any browser and finding the Network tab.
Sort by file size to find the largest listing of the PDF file, as this is most likely to be the complete file without missing pages. If you don't see it, try reloading the page. From there, try these in order, from easiest to most annoying.
Method 1: If you're really lucky there will be an accessible URL for the PDF you can download directly. Just right click and open the file in a few tab, then you can save it.
Method 2: Copy the cUrl command and pipe that to a file. In theory this may be possible from any command prompt but I've had the best luck with Linux/WSL.
Method 3: Save as HAR with content, and use something like har-extractor to extract the PDF.
I'm going to answer based on how it works for me on Firefox, but similar steps should work for other browsers.
When you view a file in PDF.js, there is a toolbar on the top of the frame. On the right side, there should be a few icons. The one with the page and the down arrow is used to download the file.
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.
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.
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 havetypeas a valid attribute, buttypedoes 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
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>
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`
);
I have struggled a bit to find a solution that works for both IE and Chrome. This works for me:
$(function() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
var trident = ua.indexOf('Trident/');
var edge = ua.indexOf('Edge/');
var url = '/url/to/file.pdf';
var pdf ='';
var style = 'position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;';
if(msie > 0 || trident > 0 || edge > 0){
pdf = '<object data="' + url + '" name="print_frame" id="print_frame" style="' + style + '" type="application/pdf">';
}
else{
pdf ='<iframe src="' + url + '" name="print_frame" id="print_frame" style="' + style + '"></iframe>';
}
$(document.body).append(pdf);
setTimeout(function(){
window.frames["print_frame"].focus();
window.frames["print_frame"].print();
},2000);
});
...cheers.
As suggested in this answer for a similar question, you could do this:
window.frames.pdfFrame.print();
This should solve your problem.
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!