Pdf 通过 Node.js 结合 C++ 实现

将Excel工作簿转换为PDF

PDF文件被广泛用于组织、政府部门和个人之间交换文档。它是一种标准文档格式,软件开发人员经常被要求找到一种方法将Microsoft Excel文件转换为PDF文档。

Aspose.Cells支持将Excel文件转换为PDF,并在转换过程中保持高度的视觉保真度。

直接转换

Aspose.Cells for Node.js via C++ 支持独立于其他软件将电子表格转换为 PDF。只需使用 Workbook 类的 save(string, SaveFormat) 方法保存Excel文件为 PDF。save(string, SaveFormat) 方法提供 SaveFormat.Pdf 枚举成员,将原生的Excel文件转换为PDF格式。

按以下步骤直接将Excel电子表格转换为PDF格式:

通过调用其空构造函数实例化Workbook类的对象。

  1. 您可以打开/加载现有模板文件,或者如果您是从头开始创建工作簿,则跳过此步骤。
  2. 使用Aspose.Cells的API在电子表格上进行任何工作(输入数据,应用格式,设置公式,插入图片或其他绘图对象等)。
  3. 完成电子表格代码后,调用 Workbook 类的 save(string, SaveFormat) 方法保存电子表格。

文件格式应为PDF,因此从SaveFormat枚举中选择Pdf(预定义值)生成最终PDF文档。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xls");

// Instantiate the Workbook object
// Open an Excel file
const workbook = new AsposeCells.Workbook(filePath);

// Save the document in PDF format
workbook.save(path.join(dataDir, "output.pdf"), AsposeCells.SaveFormat.Pdf);

高级转换

您还可以选择使用PdfSaveOptions类来设置转换的不同属性。设置PdfSaveOptions类的不同属性可控制输出PDF的打印、字体、安全和压缩设置。

最重要的属性是getCompliance(),它允许您设置PDF的标准兼容级别。目前,您可以保存为PDF 1.4、PDF 1.5、PDF 1.6、PDF 1.7、PDF/A-1a、PDF/A-1b、PDF/A-2a、PDF/A-2b、PDF/A-2u、PDF/A-3a、PDF/A-2ab和PDF/A-3u格式。请注意,使用PDF/A格式时,输出文件大小大于常规PDF文件大小。

将工作簿保存为PDF/A兼容文件

下面提供的代码片段演示了如何使用PdfSaveOptions类将Excel文件保存为PDF/A兼容的PDF格式。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiate new workbook
const workbook = new AsposeCells.Workbook();

// Insert a value into the A1 cell in the first worksheet
workbook.getWorksheets().get(0).getCells().get(0, 0).putValue("Testing PDF/A");

// Define PdfSaveOptions
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();

// Set the compliance type
pdfSaveOptions.setCompliance(AsposeCells.PdfCompliance.PdfA1b);

// Save the file
workbook.save(path.join(dataDir, "output.pdf"), pdfSaveOptions);

设置PDF创建时间

使用PdfSaveOptions类,您可以获取或设置PDF创建时间。以下代码演示了使用PdfSaveOptions.getCreatedTime()属性设置PDF文件的创建时间。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Load excel file containing charts
const workbook = new AsposeCells.Workbook(inputPath);

// Create an instance of PdfSaveOptions
const options = new AsposeCells.PdfSaveOptions();
options.setCreatedTime(new Date());

// Save the workbook to PDF format while passing the object of PdfSaveOptions
workbook.save(path.join(dataDir, "output.pdf"), options);

设置ContentCopyForAccessibility选项

使用PdfSaveOptions类,您可以获取或设置PDF的getAccessibilityExtractContent()选项,以控制转换后PDF中的内容访问。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");

// Output directory
const outputDir = path.join(__dirname, "output");

const inputPath = path.join(sourceDir, "BookWithSomeData.xlsx");

// Load excel file containing some data
const workbook = new AsposeCells.Workbook(inputPath);

// Create an instance of PdfSaveOptions and pass SaveFormat to the constructor
const pdfSaveOpt = new AsposeCells.PdfSaveOptions();

// Create an instance of PdfSecurityOptions
const securityOptions = new AsposeCells.PdfSecurityOptions();

// Set AccessibilityExtractContent to true
securityOptions.setAccessibilityExtractContent(false);

// Set the security option in the PdfSaveOptions
pdfSaveOpt.setSecurityOptions(securityOptions);

// Save the workbook to PDF format while passing the object of PdfSaveOptions
workbook.save(path.join(outputDir, "outFile.pdf"), pdfSaveOpt);

导出自定义属性到PDF

使用PdfSaveOptions类,您可以将源工作簿中的自定义属性导出到PDF。提供了PdfCustomPropertiesExport枚举用于指定属性的导出方式。这些属性可以通过单击“文件”然后选择“属性”在Adobe Acrobat Reader中观察。模板文件"sourceWithCustProps.xlsx"可在此处下载进行测试,输出的PDF文件"outSourceWithCustProps"可在此处进行分析。

todo:image_alt_text

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sourceWithCustProps.xlsx");

// Load excel file containing custom properties
const workbook = new AsposeCells.Workbook(filePath);

// Create an instance of PdfSaveOptions
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();

// Set CustomPropertiesExport property to PdfCustomPropertiesExport.Standard
pdfSaveOptions.setCustomPropertiesExport(AsposeCells.PdfCustomPropertiesExport.Standard);

// Save the workbook to PDF format while passing the object of PdfSaveOptions
workbook.save("outSourceWithCustProps.pdf", pdfSaveOptions);

转换属性

我们致力于增强每个新版本的转换功能。 Aspose.Cells的Excel转PDF转换仍然存在一些限制。 在转换为PDF格式时不支持MapChart。 还有一些绘图对象支持不佳。

下表列出了使用Aspose.Cells导出为PDF时全部或部分支持的所有功能。 该表不是最终版本,不涵盖所有电子表格属性,但确实标识了在转换为PDF时不支持或部分支持的功能。

文档元素 属性 支持 备注
对齐 支持
背景设置 支持
边框 颜色 支持
边框 线条样式 支持
边框 线宽 支持
单元格数据   是  
备注   是  
条件格式   是  
文档属性   是  
绘图对象   部分 绘图对象的阴影和3D效果支持不佳;WordArt和智能图表部分支持。
字体 大小 是  
字体 颜色 是  
字体 样式 是  
字体 下划线 是  
字体 效果 是  
图像   是  
超链接   是  
图表   部分 不支持地图图表。
合并单元格   是  
分页符   是  
页面设置 页眉/页脚 是  
页面设置 页边距 是  
页面设置 页面方向 是  
页面设置 页面大小 是  
页面设置 打印区域 是  
页面设置 打印标题 是  
页面设置 缩放 是  
行高/列宽   是  
RTL (从右到左) 语言   是  

高级主题