简体   繁体   English

如何在角度导出pdf?

[英]how to export pdf in angular?

I'm using angular7. 我正在使用angular7。 I would like to download the data in the list as a PDF. 我想将列表中的数据下载为PDF。 I tried to do this as follows, but it wasn't. 我尝试如下进行操作,但事实并非如此。 It prints the console screen as I want, but it doesn't download. 它会根据需要打印控制台屏幕,但不会下载。 npm with jsPDF and jspdf-autotable download. 使用jsPDF和jspdf-autotable下载的npm。

.ts code: .ts代码:


import { DepartmanModal} from 'app/core/departman/departman.model';
import * as jsPDF from 'jspdf';
import 'jspdf-autotable';

export class HomeComponent implements OnInit {
    departmanList: Array<DepartmanModal>;
    doc: any;
    col = ['Details', 'Values'];
    rows: Array<any> = new Array<any>();
    temp: any;

  constructor( private departmanService: DepartmanService) {
        this.departman();
    }
    departman() {
        this.departmanService.getAll().subscribe(data => {
            this.departmanList= data.body;
        });
    }

convert() {
        this.doc = new jsPDF();
        this.departmanList.forEach(element => {
            this.temp = [element.id , element.name];
            this.rows.push(this.temp);
        });
        console.log(this.doc);
        this.doc.autoTable(this.col , this.rows);
        console.log( this.doc.autoTable(this.col , this.rows)); //  It prints the console screen as I want, but it doesn't download.
        this.doc.save('home.pdf');
    }
}

.html code: .html代码:

 <div (click)="convert()" class="example">
              <span class="iconPDF"> Click for Download</span>
     </div>

Try to change your code to this to see if it work ot not 尝试将您的代码更改为此,以查看其是否有效

 import jsPDF from 'jspdf';

 let doc = new jsPDF();
 this.departmanList.forEach(element => {
    this.temp = [element.id , element.name];
    this.rows.push(this.temp);
 });

 doc.autoTable(this.col , this.rows);
 doc.save('home.pdf');

You can find example here 你可以在这里找到例子

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

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