get base64 dataURL of pdf
Is there a way to get base64URL of generated PDF using html2pdf? Moreover when I give html2pdf("element name") it automatically downloads as pdf, what I want is to store pdf for multiple elements and print as pdf at end. Thanks in advance!
Hi @chinniah-a, yes you can get the final jsPDF object (instead of auto-saving) using the pdf-callback branch. More info here. This branch hasn't been merged in yet because it's a workaround, I'm planning an API change that will solve this problem differently.
Thanks @eKoopmans ! It worked
Hi @eKoopmans , Margin is not visible when I give margin as 2, html2pdf(document.getElementById("element id"), { pdfCallback: myCallback, margin: 2 }); Thanks in advance!
The margin will be in whatever units you set for jsPDF. The default is mm, so 2mm will be very small.
Hi @eKoopmans when a textarea(which can be expanded) is a part of html, when it is extended and content is typed, I am not getting the whole expanded text area in PDF, am getting only the initial size of text area that was there before extending. why is it so?
Possibly related to #46. If your original issue is resolved (base64 dataURL), would you mind closing this issue and opening a new issue about the textareas? It would also help if you could make a Fiddle that highlights the issue. Thanks!
Sure @eKoopmans ! Thanks
@chinniah-a can you help me with store the pdf file in my server? My plan is to send the pdf file by e-mail, but i dont know what to do with the pdf object.
My actual code:
function myCallback(pdf) {
console.log(pdf);
}
var element = document.getElementById('tblBoleto');
html2pdf(element, {
margin: [10, 5, 0, -60],
filename: '<?= $dadosboleto["numero_documento"] . '-' . $dadosboleto["sacado_nome"] ?>.pdf',
image: {type: 'jpeg', quality: 0.98},
html2canvas: {dpi: 300, letterRendering: true, width: 1300, height: 1000, windowWidth: 1000, windowHeight: 1000},
jsPDF: {unit: 'mm', format: 'a4', orientation: 'portrait'},
pdfCallback: myCallback
});
@vitormc1 pdf.toDataURL() will give the data URL, and u can save it in the backend
Hi, I have just pushed a new branch, new-api, which is ready for testing! The readme has all the information on the new API. Please let me know if there are any problems with it - I want to make sure it's stable before merging into master!
For this specific request, you can now use the .output method to get a datauristring, and you can reuse the PDF object to print multiple objects before saving.
@chinniah-a can you share the solution to send generated PDF as an email attachment ..
@nauman555 html2pdf(document.getElementById("element id"), { pdfCallback: myCallback, margin: 2 });
function myCallback(pdf) { console.log(pdf.toDataUrl()); }
Try by adding a callback, pdf.toDataUrl() will give the base 64 url, which you can process it
@chinniah-a sorry i am new , the pdf.toDataUrl() gives me response like below.
0ms html2canvas: html2canvas $npm_package_version html2pdf.bundle.js:2052 2125ms html2canvas: Canvas renderer initialized (794x3985 at 286.140625,2789) with scale 1
how i extract the base url from this ?
For anyone else looking here, the solution from @chinniah-a was using the old (obsolete) "pdf-callback" version of html2pdf. The current way to get the PDF encoded as a string is:
html2pdf().from(element).toPdf().output('datauristring').then(function (pdfAsString) {
// The PDF has been converted to a Data URI string and passed to this function.
// Use pdfAsString however you like (send as email, etc)! For instance:
console.log(pdfAsString);
});
Hello eKoopmans, i m using this .output('datauristring') but getting error like DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
please help