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

Text sliced at botton of pdf

Open lidijarad opened this issue 9 years ago • 19 comments

Hello @eKoopmans,

thank you again for such a great tool. I was wondering if you you or anyone has encountered this problem. I am using html2pdf lib to produce a PDF where the html is dynamic and changing. I have tried adjusting the margins but for some cases, when the text within the html is long and needs to go onto the next page, the text on the last line is sliced. Here is an example of what I mean: Is it possible that this might be due to the docHeight of the canvas as mentioned in #6? This is how I am producing the pdf:

html2pdf(pdf_element, {
	 margin: [0.8, 1, 0.5, 1],
	 filename: pdf_name,
         image: { type: 'jpeg',quality: 0.98 },
         html2canvas: { dpi: 192, letterRendering: true },
         jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
});

screenshot from 2017-05-29 21-40-59

lidijarad avatar Jun 02 '17 09:06 lidijarad

Hi @lidijarad, yes at the moment that's intended behaviour. All html2pdf is doing is converting the webpage into an image, and then slicing that image onto separate PDF pages. If your text (or any other component) straddles two pages, it will appear sliced.

The only solution I can recommend right now is the page-breaks. You can add a <div class="html2pdf__page-break"></div> anywhere in your HTML, and any elements below it will be moved to the top of the next page. I've written a bit more about how to use them here. As I mention over there, I have plans to make the page-breaks easier to use.

eKoopmans avatar Jun 03 '17 04:06 eKoopmans

@eKoopmans, thank you for the feedback. Because the content in the html is dynamic, it is very difficult to calculate when to insert the page break. I have experimented with a few different ways, (counting number of characters in a page, estimating number of lines) but I have not found an elegant solution. I will continue working on this. If I find a solution, I will post it here. Thank you again, please keep in touch if you make any changes with regards to this.

lidijarad avatar Jun 06 '17 07:06 lidijarad

Great, thanks. I have a few ideas, but it depends on your situation. I could probably make an auto-break system that would add a page break before any element that would span two pages (as long as the element itself wasn't taller than a page). If you have spans of text longer than a page it would have to be a different solution.

eKoopmans avatar Jun 08 '17 16:06 eKoopmans

Hi, I had a similar problem printing reports in a vertically long table. I solved my it by calculating the heights of the element before I added them, adding a page-break if it would exceed the page height. It's not the most elegant solution however as it leaves a bit of a white space at the bottom of the page. An A4 page is 595pt/842pt ( 793px/1122px. )

I hope this helps, would be interested to hear the solution you choose to go with.

ABobbyT avatar Jun 09 '17 07:06 ABobbyT

Thanks @ABobbyT, yeah that's basically what I was thinking for an "auto-break" system, but I could work it into the internal guts of html2pdf.

eKoopmans avatar Jun 14 '17 04:06 eKoopmans

I'm guessing the auto-break is not there yet? It's actually the only thing I've actually found missing. Otherwise great job!

MythThrazz avatar Aug 09 '17 15:08 MythThrazz

Yes, you're right @MythThrazz! Haven't had much time to work on this project lately, unfortunately. I'm hoping to have an update soon!

eKoopmans avatar Aug 10 '17 12:08 eKoopmans

@ABobbyT @eKoopmans >>I solved my it by calculating the heights of the element before I added them Can you please give me an insight of how to calculate an element height before adding to DOM? I have multiple

s to 'echo' from a PHP loop and want to add page-break so that it does not break the text. Please advise. Thanks

itskashi avatar Jan 24 '18 16:01 itskashi

Hi , i am too facing same probs in angular6. If any got solution means pls let me know, another probs is images not rendered from api.

kkbalu avatar Aug 06 '18 13:08 kkbalu

lilidijard I have the same problem did you come up with any solutions

Greensahil avatar Sep 08 '18 05:09 Greensahil

@eKoopmans @lidijarad did you find any solution?

khateeburrehman avatar Sep 14 '18 14:09 khateeburrehman

Hello @khateeburrehman @Greensahil, I never resolved this, I'm sorry.

lidijarad avatar Sep 14 '18 15:09 lidijarad

Hi all, see #153 for an update to page-breaks that will be merged in soon.

eKoopmans avatar Sep 15 '18 14:09 eKoopmans

hi, @lidijarad did you use any other library/3rd party. how you overcome that problem. please let me know if there is. thanks in advance

khateeburrehman avatar Sep 19 '18 06:09 khateeburrehman

Hi, closing as resolved by the v0.9.1 page-break features.

eKoopmans avatar Jan 17 '19 02:01 eKoopmans

Hi Guys, i am using the same version mentioned above i.e. 'v0.9.1' but still facing this issue of page break. Does anybody have some other solution. Please share. Thanks in advance!!

amitkachi avatar Feb 10 '20 04:02 amitkachi

Can you please check this pagebreak: { mode: 'avoid-all'}, i have tested in multi page pdf consisting of 10+ pages. it works fine for me

vishnudas707 avatar Apr 25 '20 17:04 vishnudas707

Using mode : avoid-all as well doesnt work for me.. there is still text sliced at bottom

LohithaYalavarthi avatar Sep 01 '20 05:09 LohithaYalavarthi

@eKoopmans i am using this library and facing issue of large size let opt = { margin: [5, 5, 5, 5], filename: 'Xyz.pdf', image: { type: 'jpg', quality: 0.98}, html2canvas: { letterRendring: true, scale: 1.5, dpi: 192 },
jsPDF: { unit: 'mm', format: 'letter', orientation: 'portrait', compress: true }, pagebreak: { mode: 'avoid-all', after: '.breakPage' } };

client doesn't wants to compromise with the quality and they want to send the pdf over the mail. Please help me

purnimasingh2609 avatar Sep 22 '21 15:09 purnimasingh2609