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

i fixed back-break height bug

Open mooksama opened this issue 5 years ago • 3 comments

hi i fixed page-break have height is problem i used code :

        html2pdf().from(element).set({
        margin: 0,
        filename: 'test.pdf',
        html2canvas: {width: 1200},
        jsPDF: {orientation: 'portrait', unit: 'mm', format: 'a4', compressPDF: true}
        }).save();

this code is very simple and i want to use page-break but height is high then page

i fixed code in html2bundle.js

  Worker.prototype.toContainer = function toContainer() {
    ...
    var pxPageHeight = this.prop.pageSize.inner.px.height;
    var pxPageWidth = this.prop.pageSize.inner.px.width;      
  
  if (rules.before) {
    var _ratio = clientRect.width / pxPageWidth;
    var _height = pxPageHeight - ( ( clientRect.bottom / _ratio ) % pxPageHeight);
    _height *= _ratio;

    var pad = createElement('div', { style: {
        display: 'block',
        height: _height + 'px'
      } });
    el.parentNode.insertBefore(pad, el);
  }
  if (rules.after) {
    var _ratio = clientRect.width / pxPageWidth;
    var _height = pxPageHeight - ( ( clientRect.bottom / _ratio ) % pxPageHeight);
    _height *= _ratio;

    var pad = createElement('div', { style: {
        display: 'block',
        height: _height + 'px'
      } });
    el.parentNode.insertBefore(pad, el.nextSibling);
  }

it's done i was very helpful i can't speak english well but i hope it helps thanks

PageTest.pdf

mooksama avatar Sep 16 '20 04:09 mooksama

Hello @mooksama. Are you still getting this kind of result, with the text being cut between pages?

image

testaroman avatar Oct 23 '20 14:10 testaroman

I am having the same issue when I print more than 8 pages. any fix for that. when I split the div into multiple and call print for each div with addPage() solves the text cutdown, but it takes more time to print.

yamuna83 avatar Feb 01 '21 23:02 yamuna83

I had a similar issue and fixed it in toContainer as follows:

var pxPageHeight = this.prop.pageSize.inner.px.height; // Check all requested modes.
var ratio = this.prop.src.clientWidth / this.prop.pageSize.inner.px.width;
pxPageHeight *= ratio;

systemed avatar Jan 22 '25 12:01 systemed