parallax/jsPDF

HTML autoPaging by div

tlw-ryan opened this issue · 10 comments

I am having trouble with autoPaging: "text" messing up my formatting. Ideally I would like an autoPaging setting that is able to prevent whole divs from being split between pages, rather than just the text being pushed down independent of the div it is part of.

I am also open to any workaround that might be offered. I have tried get reading the height of each div and adding a spacer to push any divs that would be split to the next page, but I was never able to accurately ascertain the rendered height of the page so it never worked correctly.

CleanShot 2024-09-16 at 14 48 42@2x
CleanShot 2024-09-16 at 14 51 24@2x

<html>
<head>
  <script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>  
<script>

  var { jsPDF } = window.jspdf;
  
  var PDFCreated = false;
  var retoolModel;
  
  let generate = function() {
    
    var pdf = new jsPDF({
      orientation: "portrait",
      unit: "px",
      format: "a4"
    });
    
    const html = document.getElementById("template_contents")
  
    pdf.html(html, {
      callback: function (doc) {
        retoolModel.pdf_blob = btoa(doc.output());
        window.Retool.modelUpdate(retoolModel);
        window.Retool.triggerQuery("generate_submission_data");
        PDFCreated = true;
      },
      hotfixes: ["px_scaling"],
      autoPaging: "text",
      x: 0,
      y: 0,
      margin: [10,10,10,10],
      html2canvas:  { scale: 0.5 },
    })
  };
  
  window.Retool.subscribe(function(model) {
    if (!model) { return }
    if (PDFCreated) { return }
    
    retoolModel = model;
    const style = document.createElement("style")
    style.textContent = model.css;
    document.head.append(style);
    
    document.getElementById("template_contents").innerHTML = model.pdf_html;
    
    generate();
  });
  
</script>
</head>
<body>
  
  <!-- ... This stores the HTML content that we use to generate the PDF -->
  <div id="template_contents" style="width:850px">
    
  </div>
 </body>
</html>

lets do it 🚀

You can add a class identifier to the div tag to determine where to addPage when manually paging

You can add a class identifier to the div tag to determine where to addPage when manually paging

I had no luck getting manual paging to work well with the jspdf html function and html2canvas. The idea here is to avoid needing manual paging with html by providing a more robust "autoPaging" feature that allows more granular control over what elements get pushed to the next page.

Is the issue is still open ? , I would love to work on this .

Is the issue is still open ? , I would love to work on this .

@sanjogbhalla16 Still an issue for us. A resolution would be much appreciated!

Hi @tlw-ryan , Please assign this task to me , I'll put work into this , thank you.

Hey @tlw-ryan ! I would like to contribute in this. Can you assign it to me if its still open ?
Thank you!

@sanjogbhalla16 @Ruffzi069 It doesnt look like I have permission to assign the issue sorry

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.

Facing same issue, Any luck ?