jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

html() function always starts on first page

Open glebov21 opened this issue 4 years ago • 9 comments

Hello. Does jsPDF.html method can draw <table> in pdf? And how can i with html() draw in not first page of pdf?

glebov21 avatar Jul 08 '21 15:07 glebov21

Yes, html can draw tables.

Just call addPage/setPage before html.

HackbrettXXX avatar Jul 09 '21 08:07 HackbrettXXX

I was try, but can't draw html on second page: https://jsitor.com/OjADPcBNh

glebov21 avatar Jul 13 '21 10:07 glebov21

Ah, you're right. There seems to be a bug in jsPDF. The context2d.autoPaging property is set to true (which is probably good for most use cases): https://github.com/MrRio/jsPDF/blob/cef97fb34eda41a8704c9f3983e680919a328ce4/src/modules/html.js#L451-L454

However, the autoPaging mechanism doesn't respect the starting page. See e.g. here:

https://github.com/MrRio/jsPDF/blob/cef97fb34eda41a8704c9f3983e680919a328ce4/src/modules/context2d.js#L1675-L1681

A workaround would be to first call html with only one page and the afterwards add another page with insertPage before the first page.

HackbrettXXX avatar Jul 13 '21 12:07 HackbrettXXX

Hi, any update regarding this issue? I've been facing the same issue.. Trying to add some html content after a table in the first few pages.. But the html content always starts from the first page on top of the table contents.

mageshwaran-p avatar Jul 29 '21 06:07 mageshwaran-p

Hi, any update regarding this issue? I've been facing the same issue.. Trying to add some html content after a table in the first few pages.. But the html content always starts from the first page on top of the table contents.

While issue not fixed, i use offset by y coord:

doc.html(mdDiv,
    { 
    callback: function (doc) {doc.save(); },
    y: ((pageNum-1) * pageHeight) + offsetY,
    });

glebov21 avatar Jul 29 '21 08:07 glebov21

i want to work on this issue

kanisshka avatar Oct 01 '22 02:10 kanisshka

Hey, is this issue still valid? If so, is there a way to get a new repro link? Looks like the old link was removed (returning 404 now). I tried working on it but would be very nice to help me understand the issue and confirm it's all working once fixed.

guilhermelimak avatar Oct 17 '22 19:10 guilhermelimak

I'm still experiencing this issue in the most recent version of jspdf, though I can't offer a repro at the moment. The workaround of https://github.com/parallax/jsPDF/issues/3204#issuecomment-888925112 worked for me.

Polarisation avatar Oct 18 '22 10:10 Polarisation

The only way I found to re-execute the html function was to place the y from the height of the document. Note: you must take into account in the formula to calculate the Y, subtract the margins of the first html from the height. For example:

pdf.html(htmlContent,
        callback: (doc) => {
          doc.html(htmlContent2, {            
            y: (doc.internal.pageSize.getHeight() - MARGIN)* doc.getNumberOfPages() - 1,    
            callback: (doc) => {
              window.open(doc.output("bloburl"));
            },
          });
        },
      });

elreyponce avatar Dec 31 '22 00:12 elreyponce