关于PdfDocument

PdfDocumentPDFKit的扩展,这是一个JavaScript PDF生成库。

关键的区别是:

  • 页面分为三个部分:页眉,正文和页脚。这些部分中的每一部分都代表一个绘图区域,它有自己的绘制文本和图形的方法。
  • 可以声明性地定义页眉和页脚区域的文本,并支持用于自动页面编号的宏。
  • 图形状态管理方法(如fillColor,lineWidth)在笔和画笔实体中封装,对.NET开发人员更友好。

它提供用于绘制文本,矢量图形和图像的API。

使用PdfDocument

以下是使用PdfDocument时的典型步骤序列:

  1. 使用声明的 ended 事件处理程序创建PdfDocument类实例。包含文档数据的Blob对象将在文档呈现完成后传递给此事件处理程序,并可将其保存到文件中。
  2. 使用实例成员绘制文档。
  3. 最后,调用实例的 ** nd** 方法完成渲染并引发结束事件。

以下是生成单页空白文档所需的最小代码:

var doc = new wijmo.pdf.PdfDocument({
    ended: function (sender, args) {
        wijmo.pdf.saveBlob(args.blob, "Document.pdf");
    }
});

doc.end();
import 'bootstrap.css'; import './styles.css'; import * as pdf from '@grapecity/wijmo.pdf'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { document.querySelector('#btnExport').addEventListener('click', () => { let doc = new pdf.PdfDocument({ ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Demo page.'); // doc.end(); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity PDF Overiew</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" id="btnExport">Export</button> </div> </body> </html> body { margin-bottom: 24px; } import 'bootstrap.css'; import './styles.css'; // import * as pdf from '@grapecity/wijmo.pdf'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { export() { let doc = new pdf.PdfDocument({ ended: (sender: pdf.PdfDocument, args: pdf.PdfDocumentEndedEventArgs) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Demo page.'); // doc.end(); } } // @NgModule({ imports: [BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity PDF Overiew</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" (click)="export()">Export</button> </div> body { margin-bottom: 24px; } <template> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" @click="exportPDF">Export</button> </div> </template> <script> import 'bootstrap.css'; import Vue from 'vue'; import * as pdf from '@grapecity/wijmo.pdf'; // let App = Vue.extend({ name: 'app', methods: { exportPDF() { let doc = new pdf.PdfDocument({ ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Demo page.'); // doc.end(); } } }) // new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> body { margin-bottom: 24px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity PDF Overiew</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"></div> </body> </html> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import "./app.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as pdf from "@grapecity/wijmo.pdf"; class App extends React.Component { constructor(props) { super(props); } render() { return <div className="container-fluid"> <button className="btn btn-default" onClick={this.exportPDF.bind(this)}>Export</button> </div>; } exportPDF() { let doc = new pdf.PdfDocument({ ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Demo page.'); // doc.end(); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Grapecity Wijmo OLAP Pivot Chart Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> body { margin-bottom: 24px; }