在 Node.js 中将 PDF 转换为 PDF/A 格式

Aspose.PDF for Node.js 允许您将 PDF 文件转换为 PDF/A 符合 PDF 标准的文件。

将 PDF 转换为 PDF/A 格式

如果您想转换 PDF 文档,可以使用 AsposePdfConvertToPDFA 函数。 请查看以下代码片段,以在 Node.js 环境中进行转换。

CommonJS:

  1. 调用 require 并导入 asposepdfnodejs 模块 作为 AsposePdf 变量。
  2. 指定将要转换的 PDF 文件的名称。
  3. 调用 AsposePdf 作为 Promise 并执行文件转换操作。如果成功,则接收该对象。
  4. 调用函数 AsposePdfConvertToPDFA.
  5. 修复 PDF 文件。 因此,如果 \u0027json.errorCode\u0027 为 0,则操作结果保存在 \u0022ResultConvertToPDFA.pdf\u0022 中。 在转换过程中,会执行验证,验证结果保存在 \u0022ResultConvertToPDFALog.xml.\u0022 中。如果 json.errorCode 参数不为 0,并且相应地在您的文件中出现错误,错误信息将包含在 \u0027json.errorText\u0027 中。

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Convert a PDF-file to PDF/A(1A) and save the "ResultConvertToPDFA.pdf"*/
      /*During conversion process, the validation is also performed, "ResultConvertToPDFA.xml"*/
      const json = AsposePdfModule.AsposePdfConvertToPDFA(pdf_file, AsposePdfModule.PdfFormat.PDF_A_1A, "ResultConvertToPDFA.pdf", "ResultConvertToPDFALog.xml");
      console.log("AsposePdfConvertToPDFA => %O", json.errorCode == 0 ? [json.fileNameResult, json.fileNameLogResult] : json.errorText);
  });

ECMAScript/ES6:

  1. 导入 asposepdfnodejs 模块。
  2. 指定将要转换的 PDF 文件的名称。
  3. 初始化 AsposePdf 模块。如果成功,则接收对象。
  4. 调用函数 AsposePdfConvertToPDFA.
  5. 修复 PDF 文件。 因此,如果 \u0027json.errorCode\u0027 为 0,则操作结果保存在 \u0022ResultConvertToPDFA.pdf\u0022 中。 在转换过程中,会执行验证,验证结果保存在 \u0022ResultConvertToPDFALog.xml.\u0022 中。如果 json.errorCode 参数不为 0,并且相应地在您的文件中出现错误,错误信息将包含在 \u0027json.errorText\u0027 中。

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Convert a PDF-file to PDF/A(1A) and save the "ResultConvertToPDFA.pdf"*/
  /*During conversion process, the validation is also performed, "ResultConvertToPDFA.xml"*/
  const json = AsposePdfModule.AsposePdfConvertToPDFA(pdf_file, AsposePdfModule.PdfFormat.PDF_A_1A, "ResultConvertToPDFA.pdf", "ResultConvertToPDFALog.xml");
  console.log("AsposePdfConvertToPDFA => %O", json.errorCode == 0 ? [json.fileNameResult, json.fileNameLogResult] : json.errorText);