蜡染:Linux 上的 svg 到 pdf(无 X 服务器)

发布于 2025-01-06 23:44:43 字数 374 浏览 2 评论 0原文

类似于将 SVG 转换为 PDF 中的方法 svg2pdf 转换在 Windows 下成功运行,并且可以在生成的 pdf 中搜索文本项。它也在 Linux (Ubuntu) 下生成 pdf(使用 -Djava.awt.headless=true 以避免由于缺少 X11 窗口服务器而出现异常),但文本项不可搜索,有时甚至被编码为图像。

是否也可以在 Linux 下保留 pdf 中的文本,我是否缺少一些运行时选项?

UPD:如果找不到某种字体,我可以以某种方式强制蜡染(SVGGraphics2D 或 SVGConverter)回退到默认字体吗?

Similar to approach in Convert SVG to PDF
the svg2pdf conversion runs successfully under windows and text items are searchable in the resulting pdf. It produces pdfs (with -Djava.awt.headless=true to avoid exceptions due missing X11 window server) under linux (Ubuntu) as well, but the text items are not searchable and sometimes are even coded as images.

Is it possible to preserve text in pdf under linux as well, am I missing some runtime options?

UPD: Can I somehow force batik (SVGGraphics2D or SVGConverter) to fall back to a default font, if certain font wasn't found?

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

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

发布评论

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

评论(4

过气美图社 2025-01-13 23:44:43

按照此处的食谱解决:

http://batik.2283329.n4.nabble.com/Placing-SVG-Text-into-PDF-td3778127.html

主要步骤:

  1. 用ant编译fop all
  2. copy fop-transcoder-将名为 pdf-transcoder.jar 的 allinone.jar 放入类路径中,
  3. 将 xmlgraphics-commons-1.4.jar 从 fop 的 lib 目录复制到类路径

Solved by following the recipe here:

http://batik.2283329.n4.nabble.com/Placing-SVG-Text-into-PDF-td3778127.html

major steps:

  1. compile fop with ant all
  2. copy fop-transcoder-allinone.jar under name pdf-transcoder.jar into the classpath
  3. copy xmlgraphics-commons-1.4.jar from fop's lib directory into the classpath
海未深 2025-01-13 23:44:43

如果要将字体从 Windows 复制到 Linux,只需将 .ttf 文件复制到正确的位置即可(注意:某些字体受版权保护,需要许可才能安装)。无需将它们放入某种注册表中。

要使它们在 Java 中可用,您有两个选择:

  1. 您可以在运行 batik 之前设置环境变量 JAVA_FONTS

  2. 打开jre/lib目录中的font.properties文件,取消注释并设置为适当的字体目录:

    appendedfontpath=/usr/share/fonts/truetype
    

If you want to copy a font from Windows to Linux, you only need to copy the .ttf file to the right place (note: Some fonts are copyrighted and you need permission to install them). There is no need to put them into some kind of registry.

To make them available in Java, you have two options:

  1. You can set the environment variable JAVA_FONTS before running batik

  2. Open font.properties file in the jre/lib directory, uncommnent and set to the appropriate font directory:

    appendedfontpath=/usr/share/fonts/truetype
    
一曲琵琶半遮面シ 2025-01-13 23:44:43

尝试为您想要使用的字体添加定义(带有网址):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="745" height="300" xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">
    <desc>TEST</desc>
    <defs>
        <style type="text/css">
            @font-face {
                font-family: "font";
                src: url('COMPLETE URL TO TTF FILE eg. http://example.com/font.ttf') format('truetype');
            }
        </style>
    </defs>
    <g transform="translate(174.5 53)">
        <text font-family="font" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #333; opacity: 1;" transform="translate(-98.5 39)">
            <tspan x="0" y="-26" fill="#333">TEXT THAT YOU WANT TO DISPLAY</tspan>
        </text>
    </g>
</svg>

我也遇到了同样的问题。我在 svg 中忘记的一件事是 type="text/css"

Try to add definitions (with url) for those fonts you want to use:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="745" height="300" xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">
    <desc>TEST</desc>
    <defs>
        <style type="text/css">
            @font-face {
                font-family: "font";
                src: url('COMPLETE URL TO TTF FILE eg. http://example.com/font.ttf') format('truetype');
            }
        </style>
    </defs>
    <g transform="translate(174.5 53)">
        <text font-family="font" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #333; opacity: 1;" transform="translate(-98.5 39)">
            <tspan x="0" y="-26" fill="#333">TEXT THAT YOU WANT TO DISPLAY</tspan>
        </text>
    </g>
</svg>

I had the same problem. One thing that i forgot in svg was type="text/css".

心舞飞扬 2025-01-13 23:44:43

安装 imagemagick。然后调用convert:

convert doc.svg doc.pdf

将转换为PDF。

install imagemagick. Then call convert:

convert doc.svg doc.pdf

which will convert into a PDF.

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