简体   繁体   English

jsPDF每页如何添加html内容?

[英]How to add html content to each page of jsPDF?

I am trying to convert my html page to pdf using jsPDF.我正在尝试使用 jsPDF 将我的 html 页面转换为 pdf。 Essentially i am using the html method of jsPDF, giving it a source, and options and then in the callback function i would save the document.本质上我正在使用 jsPDF 的 html 方法,给它一个源和选项,然后在回调 function 中我将保存文档。 But i am having a problem when it comes to dividing the single html document into mulitple divs and saving each div in a page.但是在将单个 html 文档分成多个 div 并将每个 div 保存在一个页面中时,我遇到了问题。 I am trying the below code and it renders all the pages blank.我正在尝试下面的代码,它使所有页面变为空白。

My html looks like我的 html 看起来像

<div class = "resultpage" >
  <div class = "print-section-1">
     //some content
  </div>
  <div class = "print-section-2">
     //some content again
  </div>
  <div class = "print-section-3">
     //content...
  </div>
</div>

My js looks like:我的 js 看起来像:

window.jsPDF = window.jspdf.jsPDF;
let doc = new jsPDF({
                    orientation : "portrait",
                    unit : 'px',
                    format : 'a4',
                    hotfixes : ["px_scaling"],
                    putOnlyUsedFonts : true
                })
 doc.html($(".prints-section-1")[0], {
            x: 10,
            y : 10,
            margin : [50, 200, 50, 200],
            autoPaging : "text"
  })
 doc.addPage()
doc.html($(".print-section-2")[0], {
            x: 10,
            y : 10,
            margin : [50, 200, 50, 200],
            autoPaging : "text"
        })
doc.addPage()
doc.html($(".print-section-3")[0], {
            x: 10,
            y : 10,
            margin : [50, 200, 50, 200],
            autoPaging : "text"
        })
doc.save("test")

This renders all the pages empty.这会使所有页面为空。

If i modify the js, to have a chaining of callbacks like below, i am able to get the last div (print-side-2 in this case) printed but the pages previous to it are blank.如果我修改 js,使回调链如下所示,我能够打印最后一个 div(在本例中为 print-side-2),但它之前的页面是空白的。

doc.html($(".print-section-1")[0], {
            callback : function(doc) {
              doc.addPage();
              doc.html($(".print-section-2")[0], {
                 callback : function(doc) {
                    doc.save("test.pdf")
                  }
                 x: 10,
                 y : 10,
                margin : [50, 200, 50, 200],
                autoPaging : "text"
              })
            }
            x: 10,
            y : 10,
            margin : [50, 200, 50, 200],
            autoPaging : "text"
        })

Can anyone point out what i am doing wrong?谁能指出我做错了什么? I searched for solutions but many use deprecated methods like addFromHTtml, and some suggested using line breaks like "<:--ADD_PAGE>" _ and style = "page-break-before. always" but both don't work.我搜索了解决方案,但许多人使用了已弃用的方法,如 addFromHTTP,有些人建议使用换行符,如 "<:--ADD_PAGE>" _ 和 style = "page-break-before.always" 但两者都不起作用。 I looked into the documentation and it hasn't been great support.我查看了文档,但没有得到很好的支持。 Please help me.请帮我。

Referencing this answer (refrencing)参考这个答案(参考)

Just use await and make sure to return doc inside the callback只需使用await并确保在回调中return doc

something like this像这样的

var doc = new jspdf.jsPDF({
  orientation: 'p',
  unit: 'pt',
  format: 'letter'
});


var field = "<b>html test </b>";
doc.text(10, 10, "test");
//add first html
await doc.html(field, {
  callback: function (doc) {
    return doc;
  },
  width: 210,
  windowWidth: 210, 
      html2canvas: {
          backgroundColor: 'lightyellow',
          width: 210, 
          height: 150
      },
      backgroundColor: 'lightblue', 
  x: 10,
  y: 50,
  autoPaging: 'text'
});
window.open(doc.output('bloburl'));

now you can call doc.html as many times as you want for that same document现在您可以为同一文档多次调用 doc.html

Originally posted by @bakuur in https://github.com/parallax/jsPDF/issues/3074#issuecomment-1328427528最初由@bakuur 发表于https://github.com/parallax/jsPDF/issues/3074#issuecomment-1328427528

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM