batik: svg to pdf on linux (no X server)

2019-04-12 22:56发布

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?

4条回答
女痞
2楼-- · 2019-04-12 23:28

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
    
查看更多
Melony?
3楼-- · 2019-04-12 23:36

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".

查看更多
戒情不戒烟
4楼-- · 2019-04-12 23:37

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
查看更多
太酷不给撩
5楼-- · 2019-04-12 23:42

install imagemagick. Then call convert:

convert doc.svg doc.pdf

which will convert into a PDF.

查看更多
登录 后发表回答