我在用着JsPDF https://www.npmjs.com/package/jspdf将 html 内容导出到下载的 PDF。考虑以下示例,该示例获取一些 HTML 内容并将其输出到下载的 PDF 文件,使用JsPdf
import React from "react";
import { render } from "react-dom";
import { renderToString } from "react-dom/server";
import Hello from "./Hello";
import jsPDF from "jspdf";
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
const colstyle = {
width: "30%"
};
const tableStyle = {
width: "100%"
};
const Prints = () => (
<div>
<h3>Time & Materials Statement of Work (SOW)</h3>
<h4>General Information</h4>
<table id="tab_customers" class="table table-striped" style={tableStyle}>
<colgroup>
<col span="1" style={colstyle} />
<col span="1" style={colstyle} />
</colgroup>
<thead>
<tr class="warning">
<th>SOW Creation Date</th>
<th>SOW Start Date</th>
<th>Project</th>
<th>Last Updated</th>
<th>SOW End Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dec 13, 2017</td>
<td>Jan 1, 2018</td>
<td>NM Connect - NMETNMCM</td>
<td>Dec 13, 2017</td>
<td>Dec 31, 2018</td>
</tr>
</tbody>
</table>
<p>
This is a Time and Materials Statement of Work between Northwestern Mutual
Life Insurance Company and Infosys with all general terms and conditions
as described in the current Master Agreement and its related documents
</p>
</div>
);
const print = () => {
const string = renderToString(<Prints />);
const pdf = new jsPDF("p", "mm", "a4");
pdf.fromHTML(string);
pdf.save("pdf");
};
const App = () => (
<div style={styles}>
<Hello name="CodeSandbox" />
<h2>Start editing to see some magic happen {"\u2728"}</h2>
<button onClick={print}>print</button>
</div>
);
render(<App />, document.getElementById("root"));
我的应用程序有一个更复杂的解决方案,但这是为了演示我正在使用的内容类型。
在我的应用程序中,我有一些用例,其中用户上传附件(保存到 MySQL 数据库中)并且可以通过 API 进行检索。在旅程中的某个时刻,他们可以将某些内容导出为 PDF。在某些时候,他们可能希望查看 PDF 中的附件。
是否可以在 PDF 文档中嵌入二进制文件?如果是这样,该怎么办?我在 Stackoverflow 或其他地方都没有遇到过任何例子。
EDIT:当我提到“嵌入二进制文件”时,我的意思是将该文件附加到 PDF 中。例如,在 Word 中,您可以在文档中包含其他内容(图片、Excel 等)的附件,当用户双击该附件时,就会将其打开。我希望在 PDF 中做类似的事情。也许在将二进制文件附加到 PDF 之前对其进行压缩(如果可能)是有意义的。
我不确定 Adobe 在这方面有什么限制,但如果可能的话,请提供帮助。
EDIT2
我见过这个example https://stackoverflow.com/q/19065562/3480297如何添加图像 - 有没有办法扩展它,以便它接受任何文件类型而不仅仅是图像?