简体   繁体   English

将HTML模板打印为PDF-Angular 2

[英]Printing a HTML template as PDF - Angular 2

I am trying to use jsPDF to print my HTML template as a PDF, but I get an error. 我正在尝试使用jsPDF将HTML模板打印为PDF,但出现错误。 How should I do it? 我该怎么办?

Let's say that I have this template: 假设我有这个模板:

<div style="text-align:center;" class="sub-header col-lg-12 col-md-12 col-sm-12">
    <span class="welcome-message col-lg-12 col-md-12 col-sm-12">Hola mundo</span>
</div>

You can also use jsPDF. 您也可以使用jsPDF。

Here is an example using jsPDF. 这是使用jsPDF的示例

After implementing jsPDF then you also need to install html2Canvas and add it in package.json : 在实现jsPDF之后,您还需要安装html2Canvas并将其添加到package.json

"dependencies": {
"html2canvas": "0.5.0-beta4",
"@types/html2canvas": "0.5.32"
.........
}

Run npm install 运行npm install

I have used jsPDF but the printed html was quite ugly so I have decided to create the table manualy. 我使用了jsPDF,但是打印的html非常难看,所以我决定手动创建表格。

Here is an example of the frame: 这是框架的示例:

doc.setFontSize(22)
lineNumber = lineNumber - 6  // total number of mm in the table
doc.text(80, 20, 'Dayly tasks')  // Title
doc.setFontSize(12)   
doc.line(18, 30, 18, lineNumber)  //first vertical line
doc.line(192, 30, 192, lineNumber) //last vertical line
doc.line(18, lineNumber, 192, lineNumber)// last horizontal line
doc.line(18, 30, 192, 30) // first horizontal line
//drax inside the table
doc.line(18, 40, 192, 40)  // colon name
doc.line(42, 30, 42, lineNumber)  //col day
doc.line(172, 30, 172, lineNumber) // col activity
doc.line(152, 30, 152, lineNumber) // col duration
doc.setFontType("bold");
doc.text(20, 35, 'Date');
doc.text(70, 35, 'Activity Comment');
doc.text(154, 35, 'Duration');
doc.text(174, 35, 'Overtime')
doc.setFontType("normal");
doc.text(180, 280, pageNumber + '')

Where lineNumber is firstly calculated. 其中lineNumber是首先计算的。

with best regards 最诚挚的问候

At the end, what I have used is this two links: 最后,我使用的是这两个链接:

1st link It is used to check how to install the needed library: Link 第一个链接用于检查如何安装所需的库: 链接

npm install jspdf --save npm install jspdf-保存

npm install @types/jspdf --save npm install @ types / jspdf-保存

angular-cli.json 角cli.json

"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]

2nd link Used as a model to developed the needed code (sorry but this answer is in Spanish) 第二个链接用作开发所需代码的模型(很抱歉,此答案为西班牙语)

Link 链接

Component TS 组件TS

  pruebaDivAPdf() {
    var pdf = new jsPDF('p', 'pt', 'letter');
    var source = $('#imprimir')[0];

    var specialElementHandlers = {
      '#bypassme': function (element, renderer) {
        return true
      }
    };
    var margins = {
      top: 60,
      bottom: 40,
      left: 20,
      width: 522
    };

    pdf.fromHTML(
      source,
      margins.left, // x coord
      margins.top, { // y coord
        'width': margins.width,
        'elementHandlers': specialElementHandlers
      },

      function (dispose) {
        pdf.save('Prueba.pdf');
      }, margins
    );
  }

Component HTML 组件HTML

<a (click)="pruebaDivAPdf()" class="button">Pasar a PDF</a>

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

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