React html2canvas jspdf Multiple Page - javascript

I am hoping to use html2canvas and jspdf to download a multi page PDF.
I can do single pages no problem with the following:
printDocument = () => {
const input = document.getElementById('divToPrint');
html2canvas(input, {scale: 4,})
.then((canvas) => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('l', 'pt', 'a4', true);
var width = (pdf.internal.pageSize.getWidth() * 0.90);
var height = (pdf.internal.pageSize.getHeight() * 0.90);
pdf.addImage(imgData, 'PNG', 0, 0, width, height, '','FAST');
// pdf.output('dataurlnewwindow');
pdf.save("download.pdf");
});
I also know I can add a page with the following:
pdf.addPage();
If I had a three page document split into 3 divs <div id="divToPrint1"> ... </div>, <div id="divToPrint2"> ... </div>, and <div id="divToPrint3"> ... </div> how would I create and download all 3 pages together.
Any help is greatly appreciated!

Related

How to resize in 2tml2canvas and jsPDF

I am trying to use html2canvas and jsPDF to get a PDF of the content of the page,
The idea is that someone could fill data and then print the CV created, but i want to be able to create a A4 pdf from any device, i tried to read the documentation of both libraries but i can't seem to make it work:
This is the output i am getting in the mobile and desktop versions:
Link to screenshots
EDIT:
I tried to configure both html2canvas and jsPDF in PT size:
const printDocument = () => {
setPrintable(true)
const input = document.getElementById('final-section');
html2canvas(input, {
height: 2970,
width: 2100,
scale: 1,
onclone: (cloned) => {
let toPrint = cloned.querySelector("#final-section");
toPrint.style.height = '841.88pt';
toPrint.style.width = '595.27pt';
toPrint.style.position = 'absolute';
toPrint.style.top = 0;
toPrint.style.left = 0;
}
})
.then((canvas) => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
unit: 'pt',
format: 'a4'
});
pdf.addImage(imgData, 'JPEG', 0, 0, 841.88, 595.27);
// pdf.output('dataurlnewwindow');
pdf.save("download.pdf");
})
;
}
The markup of the html is a content div that contains a modifiable section and the final section that is the printable, in desktop the modifiable section has 45vw and the final section 55 vw,
In mobile each section occupy 100vh and 100vw,

export pdf for two divs inside pdf pages using domtoimage.toPng

I have two divs (div1,div2) i want to export div1 inside pdf page 1 and export div2 inside pdf page 2 my problem i can capture only one div from them using domtoimage.toPng how can i capture both of divs my code : `
<script>
$('#downloadPDF').click(function () {
domtoimage.toPng(document.getElementById('div1'))
// domtoimage.toJpeg(document.getElementById('div2'))
.then(function (blob) {
var pdf = new jsPDF('p', 'pt', [$('#div1').width(), $('#div1').height()]);
pdf.addImage(blob, 'PNG', 0, 0, $('#div2').width(), $('#div2').height());
pdf.addPage();
pdf.addImage(blob, 'PNG', 0, 0, $('#div1').width(), $('#div1').height());
pdf.save("report.pdf");
that.options.api.optionsChanged();
});
});
</script>`
You need to prepare 2 blobs to save them into PDF.
$('#downloadPDF').click(function() {
Promise.all([
domtoimage.toPng(document.getElementById('div1')),
domtoimage.toPng(document.getElementById('div2'))
])
.then(function([blob1, blob2]) {
var pdf = new jsPDF('p', 'pt', [$('#div1').width(), $('#div1').height()]);
pdf.addImage(blob1, 'PNG', 0, 0, $('#div1').width(), $('#div1').height());
pdf.addPage();
pdf.addImage(blob2, 'PNG', 0, 0, $('#div2').width(), $('#div2').height());
pdf.save("report.pdf");
that.options.api.optionsChanged();
});
});

HTML to PDF in JS using a variable

This is my first post because I am stuck and I didn't find the solution neither here or the web.
I want to convert HTML to PDF using JS. I was searching and the best option seems to be HTML2canvas and JSpdf. But the think is my HTML is storage in a variable:
var test = '<html><head><script type="text/javscript">var number = 123;</script></head><body>
<h1>"the value for number is: " + number</h1></body></html>'
My variable is much more complex and it contains CSS and styles but this is just to get the idea. Then when I try to convert this into canvas it doesn't convert.
const filename = 'ThisIsYourPDFFilename.pdf';
html2canvas(test).then(canvas => {
let pdf = new jsPDF('p', 'mm', 'a4');
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 211, 298);
pdf.save(filename);
});
Does anyone know why this happens? Maybe it is a really stupid question but I don't know how to avoid the errors.
Thank you in advance.
You use string as a parameter for html2canvas, but it takes HTML element:
html2canvas(document.body).then(function(canvas) {
document.body.appendChild(canvas);
});
Look at their documentation
I have modified your code:
const html2canvas = require("html2canvas");
const jsPDF = require("jspdf");
html2canvas(document.getElementById("screenshot"), { scale: 1 }).then(
(canvas) => {
document.body.appendChild(canvas);
const filename = "ThisIsYourPDFFilename.pdf";
let pdf = new jsPDF("p", "mm", "a4");
pdf.addImage(canvas.toDataURL("image/png"), "PNG", 0, 0);
pdf.save(filename);
document.body.removeChild(canvas);
}
);
body should contain element with id screenshot:
<div id="screenshot">
Content
</div>
UPDATE:
According to this resource jsPDF has method fromHTML, so you might not need html2canvas
var doc = new jsPDF();
var elementHTML = $('#contnet').html();
var specialElementHandlers = {
'#elementH': function (element, renderer) {
return true;
}
};
// note here that it uses html
doc.fromHTML(elementHTML, 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
// Save the PDF
doc.save('sample-document.pdf');

can't able to get the select value in JSPDF

I am trying to save my html page as a PDF domtoimage.toPng(document.getElementById('PrintForm'))
.then(function (blob) {
var pdf = new jsPDF('p', 'pt', [$('#PrintForm').width(), $('#PrintForm').height()]);
pdf.addImage(blob, 'PNG', 0, 0, $('#PrintForm').width(), $('#PrintForm').height());
pdf.save("test.pdf");
that.options.api.optionsChanged();
});</pre>
In this I can't able to get my select tag value am while am saving as pdf i will get the Default in the select tag.Like below
Try this...
var obj = $(".yourclassname")[0]
var srcwidth = obj.scrollWidth;
var pdf = new jsPDF('p', 'pt', 'a4');
pdf.html(obj, {
html2canvas: {
scale: 600 / srcwidth
//600 is the width of a4 page. 'a4': [595.28, 841.89]
},
callback: function () {
window.open(pdf.output('bloburl'));
}
});
Where yourclassname is your object in the page (or #yourobjectid). srcwidth is the width calculation so your html fits the width of the page. You might need to add some padding to the html to fit inside the pdf (I added 20px to my div). You should be using this version 1.5.3 of jsPdf for the above to work. Hope this helps (works perferctly for me). You should also be using the latest version of html2canvas 1.0.0-rc.5

Rendering HTML to PDF with images included Django

I have a django app where users would print some pages which include images as a part of Data processing.
I tried jsPDF but it didn't render my images and I ended up with just text
$(document).ready(function() {
$("#pdfDownloader").click(function() {
var doc = new jsPDF('p', 'pt', 'a4', true);
doc.fromHTML($('#renderMe').get(0), 15, 15, {
'width': 500
}, function (dispose) {
doc.save('thisMotion.pdf');
});
});
})
This was my code and it didn't render the images so do I need to change anything?
is using a Django view would solve this and is there any alternatives to xhtml2pdf as with this I need to include my CSS in the HTML file ?
fromHTML is used to get text from the HTML to form a PDF. Try using html2canvas js library instead.
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>
You can create a canvas image and add it to the PDF with code like this,
$(document).ready(function() {
$("#pdfDownloader").click(function() {
html2canvas($("#renderMe")).then(canvas => {
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jspdf('p', 'pt', 'a4', true); // A4 size page of PDF
var positionX = 0;
var positionY = 0;
pdf.addImage(contentDataURL, 'PNG', positionY, positionY, undefined, undefined)
pdf.save('thisMotion.pdf'); // Generated PDF
});
});
})
This will get you a PDF just like the HTML rendered on screen.

Categories