eKoopmans / html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.
MIT License
4.05k stars 1.37k forks source link

The output pdf does not contain external images and external styles,why? #514

Open AnAnUncle opened 2 years ago

AnAnUncle commented 2 years ago

1.Allow CORS in your server. 2.add useCORS: true const opt = { margin: 0, filename: 'myfile.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { allowTaint: true, scale: 2, useCORS: true, }, jsPDF: { unit: 'in', format: 'a4', }, }; const worker = html2pdf().set(opt).from(html);

AnAnUncle commented 2 years ago

thanks for @christiansaavedra @guttenberger

faiq-ahmad commented 1 year ago

Is it resolved now ? useCORS : true is not working. My img tag also enables CORS bt no luck

My options are:

 var opt = {
        useCORS:      true,
        enableLinks:  true,
        margin:       20,
        filename:     filename,
    };

My html of the image that is not rendering is:

<img crossorigin="true" alt="logo" src="https://domain.eu-central-1.amazonaws.com/assets/logo/company.png" class="">

faiq-ahmad commented 1 year ago

Found a Solution 👍 Add useCORS: true inside the html2canvas tag that will work.


var opt = {
        // useCORS:      true,
        enableLinks:  true,
        margin:       20,
        filename:     filename,
        html2canvas:  {useCORS:true},
    };

Complete code:


    const element = document.getElementById("invoice");

    var opt = {
        // useCORS:      true,
        enableLinks:  true,
        margin:       20,
        filename:     filename,
        html2canvas:  {useCORS:true},
    };

    html2pdf()
        .set(opt)
        .from(element)
        .save();