且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PDF 中嵌入 SVG(使用 JS 将 SVG 导出为 PDF)

更新时间:2023-02-12 21:58:44

对于任何正在寻找纯 JS 解决方案的人:PDFKit 似乎是目前从 JS 生成 PDF 的***解决方案,它支持所有开箱即用的 SVG 几何原语(包括解释 path 几何字符串).如果您不需要符号等复杂的东西,那么渲染现有 SVG 内容所需的只是一个 DOM-walker,它可以跟踪 CSS 样式和继承.

For anyone looking for a JS-only solution: PDFKit seems to be the superior solution to generate PDF from JS these days, and it supports all SVG geometry primitives (including interpreting path geometry strings) out of the box. All that would be needed to render existing SVG content would be a DOM-walker that keeps track of CSS styling and inheritance, if you do not require complex stuff like symbols etc.

我在另一个答案中提到的 jsPDF/svgToPdf 组合的粗略 SVG 支持没有成功,这两个的源代码看起来并不对我来说非常精心和完整.

I wasn't successful with the sketchy SVG support of the jsPDF/svgToPdf combo mentioned in the other answer, and the source code of these two didn't look very well-crafted and complete to me.

用法示例 JSFiddle