jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Error in .html() method when element has background-image property

Open ihor-hrechka opened this issue 6 years ago • 11 comments

I'm trying to add content from HTML element (with using .html() method) but getting html2canvas error.

Source HTML:

<div id="element-to-print" style="background: url('/Content/Images/page-template.png'); background-size: contain;"></div>

JS:

var doc = new jsPDF({ orientation: 'p', format: 'a4'  });
doc.html(document.getElementById('element-to-print'), {
   callback: function (doc) {
         doc.output('dataurlnewwindow');
   }
}

Error: image image

If remove css property background-image and background-size - everything works fine. What can be the reason?

Thank you in advance!

ihor-hrechka avatar May 16 '19 13:05 ihor-hrechka

hi,

well this is a bug. jspdf canvas has to get a attribute "ownerDocument" which should be a by reference link to document. Then probably it will break somewhere else :D

Uzlopak avatar May 16 '19 13:05 Uzlopak

Hey, I reproduce this on my local. I was able to solve the problem with the ownerDocument by editing the canvas.js file and creating a new object property on it. It seems to be working but the output PDF comes with a black figure with the sizes of the given image.

andrescst avatar Dec 12 '19 19:12 andrescst

Hey guys! Here is what I made to solve the undefined 'ownerDocument' on the canvas. https://github.com/andrescst/jsPDF/commit/c854d7b27a9a9296453560a109d22637cd6c2aee

When the background-image attribute from the HTML element to print is removed, the function "resizeImage" is not called because there is no image in the given element. However, I was not able to solve the problem with the output black figure. It seems to be a problem with an html2canvas update.

andrescst avatar Dec 30 '19 16:12 andrescst

Same issue here... can we expect a fix?

JeffDerk avatar Mar 19 '20 16:03 JeffDerk

We should probably add this fix to the next release.

HackbrettXXX avatar Mar 19 '20 16:03 HackbrettXXX

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

github-actions[bot] avatar Jul 12 '20 01:07 github-actions[bot]

I confirm the issue

HanXHX avatar Aug 28 '20 15:08 HanXHX

@andrescst could you prepare a pull request? We might also try updating to [email protected].

HackbrettXXX avatar Aug 28 '20 16:08 HackbrettXXX

Hey, I have no longer access to @andrescst account but ill be happy to do it from this one.

andruts avatar Aug 28 '20 20:08 andruts

@andruts @HackbrettXXX Is there a workaround or timeline for when this will be merged / fixed?

subourbonite avatar May 11 '21 21:05 subourbonite

@subourbonite a workaround would be to draw the background yourself. There is no timeline for a fix ATM. I personally don't have time for it currently. But any help is welcome.

HackbrettXXX avatar May 12 '21 08:05 HackbrettXXX