Rendering HTML to PDF with images included Django - javascript

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.

Related

Vue - export DOM as PDF without jsPDF

I need to create nicely formatted PDFs from website pages that vary greatly in layout and content. The closest I've come to success is with extensive #media print {} CSS on a page-by-page basis, using the browser's print dialog, then choosing 'Save As PDF' in the print dialog.
I'd like to do all that natively in the app without relying on the print dialog, and I've spent hours trying to beat jsPDF into submission with no luck. Most of my pages include jpgs and svgs as well as text and I have tried html2canvas, dompurify & canvg in conjunction with jsPDF.
I don't mind having to use lots of custom CSS to get it looking good in the PDF, but I can't even get all the elements to appear in the downloaded PDF. Most of the time it's just blank, or one random element.
Doesn't work:
downloadPDF() {
const doc = new jsPDF();
const contentHtml = this.html;
doc.fromHTML(contentHtml, 15, 15, { // error: fromHTML is not a function
width: 170,
});
doc.save("sample.pdf");
},
Also doesn't work:
downloadPDF() {
const doc = new jsPDF();
/** WITH CSS */
var canvasElement = document.createElement("canvas");
html2canvas(this.$refs.printArea, { canvas: canvasElement }).then(function (canvas) {
const img = canvas.toDataURL("image/jpeg", 0.8);
doc.addImage(img, "JPEG", 20, 20);
doc.save("sample.pdf");
});
},
Also also doesn't work:
downloadPDF() {
const pdf = new jsPDF({
orientation: "p",
unit: "pt",
format: "letter",
putOnlyUsedFonts: true,
});
pdf.setFontSize(11);
pdf.html(this.html, {
callback: function (pdf) {
pdf.save("download.pdf");
},
});
},

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');

html2canvas/jsPDF - images not showing when converting html to a pdf

I'm looking to convert some simple HTML into a PDF. It seems the easiest way to do this and keep the css styling is to use the 'html2canvas' js library to convert the html to canvas first, and then create the PDF using jsPDF.
The issue I'm getting is that I have both a background image and inline image in my HTML but neither are showing in the PDF once converted. I've created a Codepen here: https://codepen.io/adamboother/pen/NWGeqom
Here's my js that does the conversion:
function convertToPdf()
{
html2canvas(document.querySelector('#certificate')).then(canvas => {
let pdf = new jsPDF('landscape', 'mm', 'a4');
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, pdf.internal.pageSize.width, pdf.internal.pageSize.height);
pdf.save('certificate.pdf');
});
}
Has anyone found a fix for this?
I am using useCORS: true to your code and it works, assuming your image is in the server. Besides you can follow below code:
function convertToPdf()
{
html2canvas(document.querySelector('#certificate'), {useCORS: true}).then(function(canvas) {
let img = new Image();
img.src = canvas.toDataURL('image/png');
img.onload = function () {
let pdf = new jsPDF('landscape', 'mm', 'a4');
pdf.addImage(img, 0, 0, pdf.internal.pageSize.width, pdf.internal.pageSize.height);
pdf.save('certificate.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

SVG not rendering on jsPDF

I was trying to create pdf using jspdf and which is working good except svg.All svg included in the html is not showing.
$(function(){
var pdf = new jsPDF('p', 'pt', 'a4');
$("#cmd").click(function() {
pdf.addHTML($('.chart'), function() {
pdf.save('web.pdf');
});
});
})
Is there any way to fix this issue?

Categories