jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Is it possible to create page breaks in react?

Open conor-v opened this issue 3 years ago • 2 comments
trafficstars

I have read and understood the contribution guidelines. Is it possible to create page breaks in react? For the moment i got it working with a fix height setting. But if the content is larger than 1 page, the content breaks randomly and sometimes in the middle of a sentence or photo.

My project: https://codesandbox.io/s/magical-proskuriakova-z46soi?file=/src/App.js:526-865

I had found this but it doesn't work as this is html and jsx doesn't want to recognize this. https://github.com/parallax/jsPDF/blob/master/examples/html2pdf/page_break.html

My function for creating a pdf:

  const [generatePdf, setGeneratePdf] = useState(false);
  const handleDownloadPdf = () => {
    setGeneratePdf(true);
    const doc = new jsPDF();
    doc.html(document.body, {
      callback: function (doc) {
        doc.save();
        setGeneratePdf(false);
      },

      x: 0,
      y: 0,
      width: 200,
      windowWidth: 1500,
      margin: 5,
      autoPaging: true
    });
  };

My content for the pdf:

    <>
      <button onClick={() => handleDownloadPdf()}>pdf</button>
      {[...Array(3)].map((e, i) => (
        <div className="App" style={{ height: generatePdf ? 2122.5 : "auto" }}>
          <ul>
            {[...Array(80)].map((e, i) => (
              <li>{i + 1}</li>
            ))}
          </ul>
        </div>
      ))}
    </>

conor-v avatar Jul 13 '22 07:07 conor-v

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 Oct 12 '22 02:10 github-actions[bot]

Issue still exists today.

AnthonyPhan avatar Oct 16 '22 08:10 AnthonyPhan

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 Jan 16 '23 02:01 github-actions[bot]

In case anyone else ends up here, the example given uses a deprecated CSS rule. This one is working fine for me:

@media all {
  .page-break {
    display: none;
  }
}
@media print {
  // read about page breaks: https://github.com/gregnb/react-to-print#page-breaks
  .page-break {
    padding-top: 10mm;
    display: block !important;
    break-before: page;
  }
}


mattcasey avatar May 02 '23 20:05 mattcasey

In case anyone else ends up here, the example given uses a deprecated CSS rule. This one is working fine for me:

@media all {
  .page-break {
    display: none;
  }
}
@media print {
  // read about page breaks: https://github.com/gregnb/react-to-print#page-breaks
  .page-break {
    padding-top: 10mm;
    display: block !important;
    break-before: page;
  }
}

Unfortunately it doesn't work, the inside of the div gets split anyway

clcoco avatar Nov 22 '23 14:11 clcoco