在共享 Linux 服务器上将 SVG 转换为 PDF
我有一个网站,它使用 SVG 来实现交互式客户端 thingamabob。我想提供下载最终输出的 PDF 版本的选项。我可以将最终的 SVG 输出传递回服务器,我想在服务器上将其转换为 PDF,然后将其返回给客户端以供下载。
这需要在无头共享 Linux 服务器上工作,在该服务器上安装或编译要么是一个巨大的痛苦,要么是不可能的。该网站是 PHP,因此理想的解决方案是 PHP,或者使用可以轻松安装在共享网络服务器上的软件。 Python、perl 和 ruby 以及您在 Linux 机器上可能期望的常见功能都可用。涉及 cairo、脚本 inkscape 或比“FTP it up”更复杂的安装的解决方案可能已经消失。花大钱自然也就出局了。由于这是一个共享服务器,因此内存和/或 CPU 消耗大的解决方案也不再适用,因为它们往往会被杀死;这或多或少排除了蜡染。
到目前为止,我得到的最接近的是这个我可以从 PHP 驱动 XSL 转换,然后通过 ps2pdf(已安装)喷射生成的 postscript。唯一的问题是它不支持 SVG 路径 - 如果支持,那就完美了。
StackOverflow 上有很多或相关的问题,我已经阅读了所有这些问题,但它们都假设您可以安装东西,花钱,或两者兼而有之。
有没有人对此有现成的解决方案,或者我应该花一些停机时间尝试为该 XSL 转换添加路径支持?
谢谢, 邓克
I have a website which uses SVG for an interactive client side thingamabob. I would like to provide the option to download a PDF of the finished output. I can pass the final SVG output back to the server, where I want to convert to PDF, then return it to the client for download.
This would need to work on a headless shared linux server, where installation or compilation is either an enormous pain, or impossible. The website is PHP, so the ideal solution would be PHP, or use software that's easily installed on a shared webserver. Python, perl and ruby are available, along with the usual things you might expect on a linux box. Solutions that involve cairo, scripting inkscape, or installation more complex than 'FTP it up' are probably out. Spending large amounts of money are also out, naturally. As this is a shared server, memory and/or CPU hungry solutions are also out, as they will tend to get killed; this more or less rules out Batik.
The nearest that I've got so far is this XSL transform which I can drive from PHP and then squirt the resulting postscript through ps2pdf (which is already installed). The only problem with this is that it doesn't support SVG paths - if it did, it would be perfect.
There are a bunch or related questions on StackOverflow, all of which I've read through, but they all assume that you can either install stuff, spend money, or both.
Does anyone have an off-the-shelf solution to this, or should I just spend some downtime trying to add paths support to that XSL transform?
Thanks,
Dunc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我今天偶然发现了 TCPDF,如果我当时知道的话,它对于这个来说是完美的。它只是纯 PHP 类的集合,大多数事情没有外部依赖项。
它可以从头开始构建 PDF,并且您可以在其中包含几乎任何您想要的内容,包括 SVG(以及许多其他内容),如以下示例所示:
http://www.tcpdf.org/examples.php
主要项目页面在这里:
http://www.tcpdf.org/
Sourceforge 页面位于:
http: //sourceforge.net/projects/tcpdf/
I stumbled across TCPDF today which would have been perfect for this, had I known about it at the time. It's just a collection of pure PHP classes, no external dependencies for most things.
It can build PDF's from scratch and you can include pretty much anything you want in there, including SVG (amongst many, many other things), as shown in these examples:
http://www.tcpdf.org/examples.php
Main project page is here:
http://www.tcpdf.org/
Sourceforge page is here:
http://sourceforge.net/projects/tcpdf/
您可以使用Apache FOP的免费 Batik SVG 工具包,其中有一个 转码器 api 将 SVG 转换为 PDF。
下载链接
您将需要编写一点java。 此处有代码示例 - 请注意,您需要将转码器设置为
org.apache .fop.svg.PDFTranscoder
而不是 Java。您应该能够在计算机上不安装任何东西的情况下完成此操作 - 只需将 jar 拖到那里并运行脚本即可。我引用:
You can use Apache FOP's free Batik SVG toolkit which has a transcoder api to transform SVG to PDF.
download link
You will need to write a tiny bit of java. There are code examples here – note you will need to set the transcoder to
org.apache.fop.svg.PDFTranscoder
instead of Java.You should be able to do this without installing anything on your machine – just drag the jars on there and run a script. I quote:
你看过imagemagick吗?我怀疑您还需要 ghostscript 来完成循环,这可能会增加安装难度和性能一个问题。
have you looked at imagemagick? I suspect you also need ghostscript to complete the loop, which might make installation difficulty and performance a problem.
我建议尝试 princexml ,他们提供 各种插件(包括 PHP 插件),可以从 SVG/HTML/XML 输出 PDF。
I'd suggest giving princexml a try, they provide various addons (including one for PHP) and can output PDF from SVG/HTML/XML.
我在许多项目中使用了 TCPDF (http://www.tcpdf.org/),它可以在几乎每个用例。
以下是 SVG 的示例: https://tcpdf.org/examples/example_058/
和以下内容是可以帮助您的代码:
i have used TCPDF (http://www.tcpdf.org/) in many projects and it work in almost every use case.
Here is the example of SVG: https://tcpdf.org/examples/example_058/
and following is the code which can help you: