在 PDF 中嵌入 SVG(使用 JS 将 SVG 导出为 PDF) [英] Embedding SVG in PDF (exporting SVG to PDF using JS)

查看:96
本文介绍了在 PDF 中嵌入 SVG(使用 JS 将 SVG 导出为 PDF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起点:我没有可以提供静态文件以外的任何东西的服务器.我的 中有一个 SVG 元素(动态创建的),我想将其导出为矢量格式,最好是 PDF 或 SVG.

The starting points: I don't have a server that can provide anything but static files. And I have an SVG element (dynamically created) in my <body> that I want to export to a vector format, preferrably PDF or SVG.

我开始考虑使用现有的 lib jsPDF下载.它工作得很好.不幸的是,这不支持 SVG,仅支持文本.

I started looking at using the already existing lib jsPDF along with downloadify. It worked fine. Unfortunately, this does not support SVG, only text.

我已经阅读了 PDF 格式嵌入 SVG 图像的可能性,并且似乎已启用从 Acrobat Reader 5(以及 ImageViewer 插件)开始.但它不起作用.我尝试了 3 种不同的 PDF 阅读器,但都没有成功.

I've read about the PDF format's possiblities to embed SVG images, and it seems to have been enabled since Acrobat Reader 5 (along with the ImageViewer plugin). But it doesn't work. I've tried with 3 different PDF readers without success.

这是否意味着 PDF 已经放弃了对 SVG 嵌入的支持?我没有找到任何关于此的内容.

Does this mean that PDFs has dropped SVG embedding support? I haven't found anything on this.

我有两个问题;这可以解决吗?如果是,在 PDF 中嵌入 SVG 的规范是什么?有了这些信息,我就可以自己在 jsPDF 中构建这种支持.

I have two questions; can this be solved? And if yes, what are the specifications for embedding SVG inside of a PDF? With that info, I can build that support in jsPDF myself.

浏览器支持需求为 Safari、Chrome 和 Firefox.支持 SVG 的版本.

The browser support demands are Safari, Chrome and Firefox. The versions that supports SVG.

推荐答案

对于任何正在寻找纯 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

这篇关于在 PDF 中嵌入 SVG(使用 JS 将 SVG 导出为 PDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆