在 Node.js 中将 PDF 转换为 EPUB、TeX、Text、XPS
将 PDF 转换为 EPUB
EPUB 是一种由国际数字出版论坛(IDPF)制定的免费且开放的电子书标准。文件的扩展名为 .epub。 EPUB 旨在用于可重排内容,这意味着 EPUB 阅读器可以针对特定显示设备优化文本。EPUB 还支持固定布局内容。该格式旨在作为一种统一格式,供出版商和转换公司内部使用,也用于分发和销售。它取代了 Open eBook 标准。
如果您想转换 PDF 文档,可以使用 AsposePdfToEPUB 函数。 请查看以下代码片段,以在 Node.js 环境中进行转换。
CommonJS:
- 调用
require并导入asposepdfnodejs模块 作为AsposePdf变量。 - 指定将要转换的 PDF 文件的名称。
- 调用
AsposePdf作为 Promise 并执行文件转换操作。如果成功,则接收该对象。 - 调用函数 AsposePdfToEPUB.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,操作结果将保存在 “ResultPDFtoEPUB.epub”。如果 json.errorCode 参数不是 0,并且因此在你的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Convert a PDF-file to ePub and save the "ResultPDFtoEPUB.epub"*/
const json = AsposePdfModule.AsposePdfToEPUB(pdf_file, "ResultPDFtoEPUB.epub");
console.log("AsposePdfToEPUB => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- 导入
asposepdfnodejs模块。 - 指定将要转换的 PDF 文件的名称
- 初始化 AsposePdf 模块。如果成功,则接收对象。
- 调用函数 AsposePdfToEPUB.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,操作结果将保存在 “ResultPDFtoEPUB.epub”。如果 json.errorCode 参数不是 0,并且因此在你的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Convert a PDF-file to ePub and save the "ResultPDFtoEPUB.epub"*/
const json = AsposePdfModule.AsposePdfToEPUB(pdf_file, "ResultPDFtoEPUB.epub");
console.log("AsposePdfToEPUB => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
将 PDF 转换为 TeX
Aspose.PDF for Node.js 支持将 PDF 转换为 TeX。 如果您想转换 PDF 文档,可以使用 AsposePdfToTeX 函数。 请查看以下代码片段,以在 Node.js 环境中进行转换。
CommonJS:
- 调用
require并导入asposepdfnodejs模块 作为AsposePdf变量。 - 指定将要转换的 PDF 文件的名称。
- 调用
AsposePdf作为 Promise 并执行文件转换操作。如果成功,则接收该对象。 - 调用函数 AsposePdfToTeX.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,操作结果将保存到 “ResultPDFtoTeX.tex”。如果 json.errorCode 参数不是 0,并且相应地在您的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Convert a PDF-file to TeX and save the "ResultPDFtoTeX.tex"*/
const json = AsposePdfModule.AsposePdfToTeX(pdf_file, "ResultPDFtoTeX.tex");
console.log("AsposePdfToTeX => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- 导入
asposepdfnodejs模块。 - 指定将要转换的 PDF 文件的名称
- 初始化 AsposePdf 模块。如果成功,则接收对象。
- 调用函数 AsposePdfToTeX.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,操作结果将保存到 “ResultPDFtoTeX.tex”。如果 json.errorCode 参数不是 0,并且相应地在您的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Convert a PDF-file to TeX and save the "ResultPDFtoTeX.tex"*/
const json = AsposePdfModule.AsposePdfToTeX(pdf_file, "ResultPDFtoTeX.tex");
console.log("AsposePdfToTeX => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
将 PDF 转换为 TXT
如果您想转换 PDF 文档,可以使用 AsposePdfToTxt 函数。 请查看以下代码片段,以在 Node.js 环境中进行转换。
CommonJS:
- 调用
require并导入asposepdfnodejs模块 作为AsposePdf变量。 - 指定将要转换的 PDF 文件的名称。
- 调用
AsposePdf作为 Promise 并执行文件转换操作。如果成功,则接收该对象。 - 调用函数 AsposePdfToTxt.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,则操作结果保存在 “ResultPDFtoTxt.txt” 中。如果 json.errorCode 参数不为 0,并且相应地在您的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Convert a PDF-file to Txt and save the "ResultPDFtoTxt.txt"*/
const json = AsposePdfModule.AsposePdfToTxt(pdf_file, "ResultPDFtoTxt.txt");
console.log("AsposePdfToTxt => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- 导入
asposepdfnodejs模块。 - 指定将要转换的 PDF 文件的名称
- 初始化 AsposePdf 模块。如果成功,则接收对象。
- 调用函数 AsposePdfToTxt.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,则操作结果保存在 “ResultPDFtoTxt.txt” 中。如果 json.errorCode 参数不为 0,并且相应地在您的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Convert a PDF-file to Txt and save the "ResultPDFtoTxt.txt"*/
const json = AsposePdfModule.AsposePdfToTxt(pdf_file, "ResultPDFtoTxt.txt");
console.log("AsposePdfToTxt => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
将 PDF 转换为 XPS
XPS 文件类型主要与微软公司推出的 XML Paper Specification(XML 纸张规范)相关。XML Paper Specification(XPS),曾用代号 Metro,并包含下一代打印路径(NGPP)营销概念,是微软将文档创建和查看集成到 Windows 操作系统中的倡议。
Aspose.PDF for Node.js 提供了将 PDF 文件转换为 XPS 格式。让我们尝试使用提供的代码片段将 PDF 文件转换为 XPS 格式(使用 Node.js)。
如果您想转换 PDF 文档,可以使用 AsposePdfToXps 函数。 请查看以下代码片段,以在 Node.js 环境中进行转换。
CommonJS:
- 调用
require并导入asposepdfnodejs模块 作为AsposePdf变量。 - 指定将要转换的 PDF 文件的名称。
- 调用
AsposePdf作为 Promise 并执行文件转换操作。如果成功,则接收该对象。 - 调用函数 AsposePdfToXps.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,则操作结果保存在 “ResultPDFtoXps.xps” 中。如果 json.errorCode 参数不为 0,并相应地在您的文件中出现错误,则错误信息将包含在 ‘json.errorText’ 中。
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Convert a PDF-file to Xps and save the "ResultPDFtoXps.xps"*/
const json = AsposePdfModule.AsposePdfToXps(pdf_file, "ResultPDFtoXps.xps");
console.log("AsposePdfToXps => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- 导入
asposepdfnodejs模块。 - 指定将要转换的 PDF 文件的名称。
- 初始化 AsposePdf 模块。如果成功,则接收对象。
- 调用函数 AsposePdfToXps.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,则操作结果保存在 “ResultPDFtoXps.xps” 中。如果 json.errorCode 参数不为 0,并相应地在您的文件中出现错误,则错误信息将包含在 ‘json.errorText’ 中。
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Convert a PDF-file to Xps and save the "ResultPDFtoXps.xps"*/
const json = AsposePdfModule.AsposePdfToXps(pdf_file, "ResultPDFtoXps.xps");
console.log("AsposePdfToXps => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
将 PDF 转换为灰度 PDF
使用 Aspose.PDF for Node.js via C++ 工具包将 PDF 转换为黑白。 为什么要将 PDF 转换为灰度?如果 PDF 文件包含大量彩色图像且文件大小比颜色更重要,转换可以节省空间。如果在黑白模式下打印 PDF 文件,转换可以让您直观地查看最终效果。
如果您想转换 PDF 文档,可以使用 AsposePdfConvertToGrayscale 函数。 请查看以下代码片段,以在 Node.js 环境中进行转换。
CommonJS:
- 调用
require并导入asposepdfnodejs模块 作为AsposePdf变量。 - 指定将要转换的 PDF 文件的名称。
- 调用
AsposePdf作为 Promise 并执行文件转换操作。如果成功,则接收该对象。 - 调用函数 AsposePdfConvertToGrayscale.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,则操作结果保存在 “ResultConvertToGrayscale.pdf” 中。如果 json.errorCode 参数不为 0,并且相应地在您的文件中出现错误,则错误信息将包含在 ‘json.errorText’ 中。
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Convert a PDF-file to grayscale and save the "ResultConvertToGrayscale.pdf"*/
const json = AsposePdfModule.AsposePdfConvertToGrayscale(pdf_file, "ResultConvertToGrayscale.pdf");
console.log("AsposePdfConvertToGrayscale => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- 导入
asposepdfnodejs模块。 - 指定将要转换的 PDF 文件的名称。
- 初始化 AsposePdf 模块。如果成功,则接收对象。
- 调用函数 AsposePdfConvertToGrayscale.
- 转换 PDF 文件。因此,如果 ‘json.errorCode’ 为 0,则操作结果保存在 “ResultConvertToGrayscale.pdf” 中。如果 json.errorCode 参数不为 0,并且相应地在您的文件中出现错误,则错误信息将包含在 ‘json.errorText’ 中。
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Convert a PDF-file to grayscale and save the "ResultConvertToGrayscale.pdf"*/
const json = AsposePdfModule.AsposePdfConvertToGrayscale(pdf_file, "ResultConvertToGrayscale.pdf");
console.log("AsposePdfConvertToGrayscale => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);



