@microexcel-csd/nativescript-create-pdf
一个用于在用户设备上创建和写入 PDF 文件的 NativeScript 插件。支持 Android 和 iOS。
npm i --save @microexcel-csd/nativescript-create-pdf

Nativescript Create PDF

一个插件,可以帮助创建并将包含文本和图像的 PDF 文件保存到用户设备上。

先决条件 / 需求

从:https://npmjs.net.cn/package/nativescript-permissions 安装 Nativescript-permissions 插件

npm i nativescript-permissions

如果未获得对文件系统进行读和写权限,则不会写入 PDF。

安装

tns plugin add @microexcel-csd/nativescript-create-pdf

用法

将插件导入到组件中

import { CreatePdf } from "@microexcel-csd/nativescript-create-pdf";

初始化插件

createPdf = new CreatePdf();

请求适当的 Android 权限

 ngOnInit(): void {
permissions.requestPermission([
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE",
], "We need these permissions to save pdf")
.then(function (res) {
})
.catch(function () {
});
}

创建您的 PDF 文件设置

fileSettings = {
orientation: 'portrait',
units: 'mm',
paperSize: 'Letter',
fileName: 'pdf-export.pdf', // Name your file with the extension
folderName: 'App-Exports' // Name the folder being created where your pdf will be saved
}

添加您的文本

text = [
{
data: 'Page 1 Sample Text', // this is what appears on the page
xPos: 10, // x coordinate from top left
yPos: 20, // y coordinate from top left
fontSize: 18,
pageNum: 1 // the page your text will appear on
},
{
data: 'Page 2 Sample Text',
xPos: 10,
yPos: 20,
fontSize: 14,
pageNum: 2
}
];

更改字体颜色

fontColor 添加到您的文本对象中,并用数组中的 RGB 颜色值。如果未包含 fontColor,则默认颜色为黑色。

fontColor: [8, 134, 255]

添加您的图像

images = [
{
path: './app/assets/image.jpg', // where your image is located
type: 'JPEG', // img type
xPos: 10, // x coordinate from top left
yPos: 40, // y coordinate from top left
width: 30, // width of your image in same unit size as your file settings (mm in this example)
height: 30, // height of your image in same unit size as your file settings (mm in this example)
alias: null,
compression: 'FAST',
pageNum: 1 // the page your image will appear on
},
{
path: './app/assets/image.jpg',
type: 'JPEG',
xPos: 10,
yPos: 40,
width: 100,
height: 125,
alias: null,
compression: 'FAST',
pageNum: 2
}
];

传递数据并创建您的 pdf

this.createPdf.create(this.fileSettings, this.text, this.images);
// this will create your pdf and write it to the device

致谢

https://npmjs.net.cn/package/jspdf https://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html

ISC 许可证

本许可证授予任何人使用、复制、修改和/或以任何形式(无论是否收费)分发本软件的权利,前提是上述版权声明和本许可证声明出现在所有副本中。

本软件按“原样”提供,作者不提供任何明示或暗示的保证,包括但不限于本软件的适销性和适用性。在任何情况下,作者均不对任何特殊、直接、间接或后果性损害或任何损害(包括但不限于使用本软件造成的损失、数据或利润损失)承担责任,无论此类损害是基于合同、疏忽或其他侵权行为,不论是否因使用或运行本软件而产生。

支持我们的包

paypal