html2pdf.js icon indicating copy to clipboard operation
html2pdf.js copied to clipboard

get base64 dataURL of pdf

Open chinniah-a opened this issue 9 years ago • 15 comments

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!

chinniah-a avatar Dec 04 '17 13:12 chinniah-a

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.

eKoopmans avatar Dec 05 '17 06:12 eKoopmans

Thanks @eKoopmans ! It worked

chinniah-a avatar Dec 05 '17 06:12 chinniah-a

Hi @eKoopmans , Margin is not visible when I give margin as 2, html2pdf(document.getElementById("element id"), { pdfCallback: myCallback, margin: 2 }); Thanks in advance!

chinniah-a avatar Dec 06 '17 03:12 chinniah-a

The margin will be in whatever units you set for jsPDF. The default is mm, so 2mm will be very small.

eKoopmans avatar Dec 06 '17 15:12 eKoopmans

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?

chinniah-a avatar Dec 07 '17 10:12 chinniah-a

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!

eKoopmans avatar Dec 08 '17 06:12 eKoopmans

Sure @eKoopmans ! Thanks

chinniah-a avatar Dec 15 '17 07:12 chinniah-a

@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 avatar Jan 25 '18 14:01 vitormc1

@vitormc1 pdf.toDataURL() will give the data URL, and u can save it in the backend

chinniah-a avatar Jan 30 '18 10:01 chinniah-a

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.

eKoopmans avatar Feb 24 '18 16:02 eKoopmans

@chinniah-a can you share the solution to send generated PDF as an email attachment ..

nauman555 avatar Nov 21 '18 08:11 nauman555

@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 avatar Nov 21 '18 09:11 chinniah-a

@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 ?

nauman555 avatar Nov 21 '18 11:11 nauman555

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

eKoopmans avatar Jan 10 '19 09:01 eKoopmans

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

hmusaji avatar Mar 14 '19 11:03 hmusaji