简体   繁体   English

Angular2 jsPdf生成整个div的pdf

[英]angular2 jsPdf generate pdf of entire div

I am trying to generate pdf of my view with jsPdf, which is successful except for the fact that it generates pdf of the part which is in view and not the entire html. 我正在尝试使用jsPdf生成我的视图的pdf,这是成功的,除了它生成视图中的部分pdf而不是整个html的事实。

<div style="padding: 1%;">
    <button mdl-button mdl-button-type="raised" (click)="download()">download </button>
</div>

<div class="mdl-grid" id='entireDownload'>

... 
... => here is a big html whose content do not fit in 100vh viewport

</div>

and my component: 和我的组件:

  download() {

    //var doc = new jsPDF();
    const elementToPrint = document.getElementById('entireDownload'); //The html element to become a pdf
    const pdf = new jsPDF('p', 'pt', 'a4');
    pdf.addHTML(elementToPrint, () => {
      // Save the PDF
      pdf.save('behavior-variables.pdf');
    });

  }

How can I make sure that my download function generates pdf of the entire div and not just the content which is visible at once? 如何确保我的下载功能生成整个div的pdf文件,而不仅仅是立即可见的内容?

In theory, you can instantiate your jsPDF object with the desired dimensions. 从理论上讲,您可以使用所需的尺寸实例化jsPDF对象。 Those dimensions must meet your DIV's ones: 这些尺寸必须符合您DIV的尺寸:

var pdfDoc = new jsPDF('p', 'pt', [divWidth, divHeight]);

But for any reason it doesn't work for me: the Height is always wrong 但是由于任何原因它对我都不起作用:高度总是错误的

My work around is to delete the first page and add a new one with the expected dimensions: 我的解决方法是删除第一页并添加具有预期尺寸的新页面:

var pdfDoc = new jsPDF('p', 'pt', [0, 0]);  //Units 'pt' are important!
pdfDoc.deletePage(1);
pdfDoc.addPage(myDivWidth, myDivHeight);

The 'pt' (points) units will be applied to any new page and it will fit your DIV pixels dimensions “磅”(点)单位将应用于任何新页面,并且将适合您的DIV像素尺寸

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

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