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

发布于 2024-11-05 08:36:50 字数 628 浏览 1 评论 0原文

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

我开始考虑使用现有的库 jsPDF 以及 下载。效果很好。不幸的是,这不支持 SVG,只支持文本。

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

这是否意味着 PDF 已放弃 SVG 嵌入支持?我还没有找到任何关于这方面的东西。

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

支持的浏览器要求是Safari、Chrome和Firefox。支持 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.

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

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.

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

春夜浅 2024-11-12 08:36:50

对于任何寻找纯 JS 解决方案的人来说: PDFKit 似乎是生成的最佳解决方案如今来自 JS 的 PDF,它开箱即用地支持所有 SVG 几何基元(包括解释 path 几何字符串)。如果您不需要符号等复杂的东西,渲染现有 SVG 内容所需的一切将是一个 DOM-walker,它可以跟踪 CSS 样式和继承。

我对 jsPDF/svgToPdf 组合,这两个组合的源代码对我来说看起来不是很精心制作和完整。

编辑:用法示例 JSFiddle

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.

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.

Edit: Usage example JSFiddle

王权女流氓 2024-11-12 08:36:50

jsPDF 有一个插件:svgToPdf

https://github.com/ahwolf/jsPDF/blob/master/jspdf.plugin.svgToPdf.js

我没试过,但这可能允许放弃使用外部 API 和/或必须依赖服务器端解决方案。

jsPDF has a plugin for that: svgToPdf:

https://github.com/ahwolf/jsPDF/blob/master/jspdf.plugin.svgToPdf.js

I haven't tried it, but this could allow discarding the use of an external API and/or having to rely on a server-side solution.

時窥 2024-11-12 08:36:50

我会回答我自己的问题。我最终使用了可以从 JavaScript 调用客户端的 DocRaptor 。这在技术上解决了我的问题,尽管它是一个非免费的解决方案。如果我的答案可能是服务器端解决方案,我可以使用 wkhtmltopdf 作为 由 Mic 提议

I'll reply to my own question. I ended up using DocRaptor that can be called client-side from JavaScript. This technically solves my problem, even though it is a non-free solution. If I the answer could be a server-side-solution, I could use wkhtmltopdf as proposed by Mic.

感情洁癖 2024-11-12 08:36:50

您尝试过 WKHTMLTOPDF 吗?它是一个基于webkit的免费工具。
我们写了一个小教程 此处。

在 Mac 上,使用 Safari 或 Chrome,您可以将嵌入 SVG 的 HTML 页面保存为 PDF。
由于这些浏览器在内部使用 WKHTMLTOPDF,因此这可能适合您,因为出色地。

Did you try WKHTMLTOPDF? It's a free tool based on webkit.
We wrote a small tutorial here.

On a Mac, with Safari or Chrome, you can save an HTML page with embedded SVG to a PDF.
Since these browsers use WKHTMLTOPDF internally, may be this will work for you as well.

面犯桃花 2024-11-12 08:36:50

使用 pdfkitsvg-to-pdfkitblob-stream 的最小示例,将 SVG DOM 元素下载为 PDF。

  1. index.html 中导入包。
<script src="https://cdn.jsdelivr.net/npm/[email protected]/js/pdfkit.standalone.js"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/source.js"></script>
  1. 运行此函数,其中 svg 是 SVG DOM 元素或 SVG 字符串。
function downloadPDF(svg, outFileName) {
    let doc = new PDFDocument({compress: false});
    SVGtoPDF(doc, svg, 0, 0);
    let stream = doc.pipe(blobStream());
    stream.on('finish', () => {
      let blob = stream.toBlob('application/pdf');
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = outFileName + ".pdf";
      link.click();
    });
    doc.end();
}

Minimal example using pdfkit, svg-to-pdfkit and blob-stream, that downloads an SVG DOM element as a PDF.

  1. Import packages in your index.html.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/js/pdfkit.standalone.js"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/source.js"></script>
  1. Run this function, where svg is an SVG DOM element, or SVG string.
function downloadPDF(svg, outFileName) {
    let doc = new PDFDocument({compress: false});
    SVGtoPDF(doc, svg, 0, 0);
    let stream = doc.pipe(blobStream());
    stream.on('finish', () => {
      let blob = stream.toBlob('application/pdf');
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = outFileName + ".pdf";
      link.click();
    });
    doc.end();
}
攒眉千度 2024-11-12 08:36:50

EVO HTML 到 PDF 转换器支持将 HTML 中嵌入的 SVG 转换为 PDF。您可以在此处查看此功能的示例: http://www.evopdf.com /demo/HTML_to_PDF/HTML5_Features/SVG_to_PDF.aspx 。其示例代码是:

// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

// Convert the HTML page with SVG to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(urlHtmlWithSVG);

// Send the PDF as response to browser

// Set response content type
Response.AddHeader("Content-Type", "application/pdf");

// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=SVG_to_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));

// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);

// End the HTTP response and stop the current page processing
Response.End();

EVO HTML to PDF Converter has support for converting the SVG embedded in HTML to PDF. You can see an example for this feature here : http://www.evopdf.com/demo/HTML_to_PDF/HTML5_Features/SVG_to_PDF.aspx . The sample code for this is:

// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

// Convert the HTML page with SVG to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(urlHtmlWithSVG);

// Send the PDF as response to browser

// Set response content type
Response.AddHeader("Content-Type", "application/pdf");

// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=SVG_to_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));

// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);

// End the HTTP response and stop the current page processing
Response.End();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文