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

陌路散爱 提交于 2019-12-06 09:53:25

问题


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?


回答1:


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



回答2:


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
    



回答3:


install imagemagick. Then call convert:

convert doc.svg doc.pdf

which will convert into a PDF.




回答4:


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



来源:https://stackoverflow.com/questions/9312857/batik-svg-to-pdf-on-linux-no-x-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!