eKoopmans / html2pdf.js

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

PDF does not show images from online #141

Closed codemaster730 closed 6 years ago

codemaster730 commented 6 years ago

Hi , I use Angular 5 and this is used for my Angular method.

        var element = document.getElementById('document');
        var opt = {
            margin:       [10, 0, 10, 0],
            filename:     'document.pdf',
            image:        { type: 'jpeg', quality: 0.98 },
            html2canvas:  { scale: 2 },
            jsPDF:        { unit: 'mm', format: 'letter', orientation: 'portrait' }
        };
        html2pdf().from(element).set(opt).save();

After exporting a pdf, I am unable to see any images from online but it shows locally stored images. Here is the example of image online. https://s3.amazonaws.com/images.anterasoftware.com/5b6c5886622d5Screen_Shot_2018-06-27_at_10.37.03_AM.png

Please help, @eKoopmans Regards.

codemaster730 commented 6 years ago

I am trying to render cross-domain image which is exactly stored in amazon s3. Please find the attached screenshot , any advice would be appreciated. image

phuocdh23 commented 6 years ago

I got the same issue. But it's quite different. PDF created but it show empty space, not image.

dandocan commented 6 years ago

I have the same issue as solomondn on React and can't work around it since I can't store the images locally.

dandocan commented 6 years ago

//this is my code for it

printDocuments() {
        var opt = {
            margin: 1,
            pagespilt: true,
            filename: 'download.pdf',
            image:{type:'png'},
            html2canvas: {scale: 2},
            // jsPDF:{ unit: 'in', format: 'letter', orientation: 'portrait' }
        }
        var input = document.getElementById("pdfContainer");
        console.log(input)
        html2pdf(input, opt)
    }
PTKC commented 6 years ago

I am also having a similar issue. Wont detect any external images at all.

As a workaround you can convert your images to Base64

For example.

Using

<template>
  <div id="sheet">
    <p>Hello
      <img src="{link to external Image}" />
    </p>
  </div>

  <button click.delegate="download()">Download</button>
</template>

Yields

489ms html2canvas: Finished loading 0 images []

Using Base64 (Or locally stored images)

  <div id="sheet">
    <p>Hello
      <img src="{image as base64}" />
    </p>
  </div>
272ms html2canvas: Added image data:image/gif;base64,...........

@axiom88-guru Does this do anything for you?

<img crossorigin="*" src="https://s3.amazonaws.com/images.anterasoftware.com/5b6c5886622d5Screen_Shot_2018-06-27_at_10.37.03_AM.png" />

Once again, You can only retrieve images with servers that have CORS enabled.

codemaster730 commented 6 years ago

@PhilipTKC thanks for your response. I already solved this problem by enabling CORS on S3. Thanks.

khaukheng commented 5 years ago

I got the same issue. But it's quite different. PDF created but it show empty space, not image.

I got the exact same issue with solomondn, have you fix it?

bdwellons commented 5 years ago

Converting the image to Base64 sorted me out.

gvsakhil commented 4 years ago

@bdwellons can u share me the code u used to convert images to base64?

paytah232 commented 3 years ago

@bdwellons +1 to the code. I tried in JS doing img.src=, the html page showed the image correctly, but it still wouldn't show in the generated PDF.

brunaschneiders commented 3 years ago

@bdwellons +1 to the code. I tried in JS doing img.src=, the html page showed the image correctly, but it still wouldn't show in the generated PDF.

Hi! do you have the code that can be used to convert images to base64?

InSyn commented 3 years ago

Try to specify "allowTaint" parameter to "true" in html2canvas object

opt = { margin: 0, filename: "myfile.pdf", image: { type: "jpeg", quality: 0.98 }, html2canvas: { allowTaint: true, scale: 2 }, jsPDF: { unit: "in", format: "a4", orientation: this.setup.orientation } };

PrasanthPerumalsamy commented 2 years ago

In Angular it is worked for me after doing this...

  1. add ' crossorigin="" ' in <img src="" crossorigin="" > tag
  2. add ' {useCORS: true} ' in html2canvas --> html2canvas(document.querySelector('#pdf'), {useCORS: true})
Ankit-Mishra07 commented 2 years ago

Thank you @PrasanthPerumalsamy

surojiddin commented 2 years ago
function exportToPDF() {
  let elem = document.getElementById('pdf')
  let opt = {
    filename: 'sold_products.pdf',
    image: { type: 'jpeg' },
    html2canvas: { useCORS: true, scale: 2 }
  }
  html2pdf().from(elem).set(opt).save()
}

This my code 100% best working!

cgutierrez365 commented 1 year ago

To anyone wondering how to enable CORS (Cross-origin resource sharing) on Firebase storage so your app can access photos saved there using this plugin by simply writing something like <img src=https://firebasestorage.googleapis.com/path-to-image>, follow these instructions to enable CORS. You will need to create a cors.json file with the contents in the provided example (or replace the * in "origin": ["*"] with the path to your app's domain. From what I understand specifying the specific domain is more secure because otherwise any domain, including malicious ones, can access data from your firebase storage). You can keep the cors.json file in your project's root directory. Then after installing the gsutil command line tool, you can run gsutil cors set cors.json gs://<your-cloud-storage-bucket> like the instructions say. You can find what <your-cloud-storage-bucket> is for your specific project by logging into your project on console.firebase.google.com and navigating to Storage>Files and copying what should look similar to gs://exampleproject.appspot.com.

storage bucket

Here's a link to an additional resource you may find useful: https://stackoverflow.com/a/37765371/17246058

You will also need to make sure you set useCORS: true in your pdf options:

const pdfOptions = {
          filename: 'healthreport.pdf',
          margin: [10, 10, 10, 10], // Replace with the desired margins (top, right, bottom, left)
          image: { type: 'jpeg', quality: 0.98 },
          html2canvas: { scale: 2, useCORS: true },
          jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
 };

As an example, you can create a pdf with this plugin by then putting: const pdfBlobPromise = html2pdf().set(pdfOptions).from(html).output('blob');

AhmedMuhammedElsaid commented 1 year ago

I had the same issue and the following configs work with me just fine // @ts-ignore import("html2pdf.js").then(html2pdf => { html2pdf .default() .from(wrapper) .set({ filename: "analytics.pdf", html2canvas: { dpi: 300, letterRendering: true, useCORS: true, scale: 2, }, jsPDF: { unit: "in", format: "letter", orientation: "portrait" }, pagebreak: { mode: "avoid-all", before: "#page2el" }, image: { type: "jpeg", quality: 1 }, }) .save()