如何使用JavaScript通过C++将HTML转换为PDF
Contents
[
Hide
]
概述
Aspose.Cells is a professional solution that allows you to generate PDF files from web pages and raw HTML code in your applications.
This article explains how to 将 HTML 转换为 PDF. It covers the following topics.
JavaScript中的HTML到PDF转换
如何转换HTML为PDF?借助Aspose.Cells for JavaScript via C++库,您可以轻松地通过几行代码以编程方式将HTML转换为PDF。Aspose.Cells for JavaScript via C++能够构建跨平台应用,具备生成、修改、转换、渲染和打印所有Excel文件的能力。
JavaScript 转换 HTML 为 PDF
以下JavaScript示例展示如何使用Aspose.Cells for JavaScript via C++将HTML文档转换为PDF。
- 创建一个HtmlLoadOptions类的实例。
- 初始化Workbook对象。
- 通过调用Workbook.save()方法保存输出PDF文档。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells HTML to PDF Example</title>
</head>
<body>
<h1>Convert HTML to PDF using Aspose.Cells</h1>
<input type="file" id="fileInput" accept=".html,.htm" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an HTML file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Loads the workbook which contains hidden external links
const options = new AsposeCells.HtmlLoadOptions(AsposeCells.LoadFormat.Html);
const workbook = new Workbook(new Uint8Array(arrayBuffer), options);
// Save as PDF
const outputData = workbook.save(SaveFormat.Pdf);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF generated successfully! Click the download link to get the file.</p>';
});
</script>
</html>
尝试在线将HTML转换为PDF
Aspose.Cells for JavaScript via C++ presents you online free application “HTML转PDF”, where you may try to investigate the functionality and quality it works.
