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

PDF does not display image

Open gonzaRey opened this issue 7 years ago • 10 comments

so this is my HTML

<div class="row" #coverPage>
      <div class="col-12 enhancement-paragraph cover-paragraph-space">
            <img src="/assets/images/rp-logo-cover.svg" width="25%">
         </div>
         <div class="col-12 enhancement-paragraph cover-paragraph-space">
            <h3 style="color:#0076CC"> Product Release Notes</h3>
        </div>
    </div>

and my JS:

public prtintPDF() {
    let element = this.coverPage.nativeElement;
    html2pdf(element, {
      margin:       0.5,
      filename:     'test.pdf',
      image:        { type: 'jpeg', quality: 0.98 },
      html2canvas:  { dpi: 192, letterRendering: true, proxy: 'http://localhost:4201', allowTaint:true,useCORS:true, imageTimeout:	0  },
      jsPDF:        { unit: 'in', format: 'letter', orientation: 'portrait' }
    });
  }

I am using angular 4.

as you can see on the setting I tried out everything that I have seen in the issues and in the documentation. added the proxy even when it is in the same filesystem as the image. Also, added the cors and the tainted properties. I have tried using them in a different order as well. plz help @eKoopmans

gonzaRey avatar Mar 20 '18 21:03 gonzaRey

I live the same problem myself.

I am using vuejs

@eKoopmans please help.

alicankahramaner avatar Apr 13 '18 08:04 alicankahramaner

What browser are you using? I am having a similar issue in firefox specifically but not chrome. (both on latest version)

SteveKipp avatar Apr 18 '18 21:04 SteveKipp

I using chrome and safari

alicankahramaner avatar Apr 22 '18 14:04 alicankahramaner

  • don't use overflow style
  • be sure images have same cors origin

oscar-gardiazabal avatar Mar 10 '19 09:03 oscar-gardiazabal

Any fix for this issue? I'm experiencing this same problem😔

champi-dev avatar Jun 24 '19 19:06 champi-dev

Anyone having this issue can use this workaround

toDataUrl = (url, callback) => { const xhr = new XMLHttpRequest(); xhr.onload = () => { const reader = new FileReader(); reader.onloadend = () => { callback(reader.result); }; reader.readAsDataURL(xhr.response); }; xhr.open('GET', url); xhr.responseType = 'blob'; xhr.send(); };

_renderImages(images) { const { classes } = this.props; return images.map((image, key) => { this.toDataUrl(image.name, (myBase64) => { }); return ( <img alt="" key={key} className={classes.image} src={image.name} /> ) }); }

before rendering images just convert that image into blob . It has worked in my case

wattsparas avatar Nov 14 '19 09:11 wattsparas

There's no issue here, it's an expected CORS functionality. Just replace your image's URI accordingly.

smartITNinja avatar Nov 22 '19 14:11 smartITNinja

Two things got me fixing this:

  1. Allow CORS in your server. In my case I use AWS S3, so I went to configuration and there you have the instruction to activate it

  2. Add a "crossorigin" attribute with a "*" value to your img html tags. I used javascript for this:

let imagenes = document.getElementsByTagName("img"); for (let i = 0; i < imagenes.length; i++) { imagenes[i].setAttribute("crossorigin", "*");

christiansaavedra avatar May 20 '20 18:05 christiansaavedra

i had this issue then I converted images to Blob and this is only solution , if you are run time displying pdf then try

  const file = image.target.files[0]
  let blob= URL.createObjectURL(file);
  
  or get blob from server side otherwise you'd end up getting CORS issues.

buildsomethingdifferent avatar Feb 06 '24 07:02 buildsomethingdifferent

we are also having the same issue. Is there any workaround for this issue?

rajeshwarpatlolla avatar Apr 02 '24 15:04 rajeshwarpatlolla