Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG to PDF converter that preserves text

I'm looking for a SVG to PDF converter that preserves the text in the SVG. I've tried Batik, Inkscape, and CairoSVG. The PDF generated by all of them is a bitmap image, including the text; the text cannot be selected/searched in a PDF viewer. All of them don't do a great job either, especially CairoSVG.

I followed the directions here (note that you don't have to compile FOP - you can download the PDF transcoder from here). Now I see that if I zoom into the PDF, the clarity is preserved, which I assume means the text is preserved. However, I cannot search or select the text.

Also, I compared the output of using PDF transcoder from FOP versus what's in Batik, and I see no difference.

like image 297
user2233706 Avatar asked Apr 06 '13 19:04

user2233706


People also ask

Can you turn an SVG into a PDF?

Convert SVG to PDF in Inkscape Open Inkscape and click File->Open to load the file for conversion. Click File then Print or press Ctrl+P and in the Print window select novaPDF as the printer. Click on Print and the SVG will be saved as PDF.

How do I merge SVG files to PDF?

How can I convert SVG files to PDF? Use the file selection box to select the SVG files you want to convert to PDF format. Start the conversion of your SVG files by clicking the Convert button. Save the converted SVG files as PDF by using the download button.


1 Answers

If you're using filters, gradients or masking, it might be that it's impossible to translate this 1:1 to PDF. In these cases, converters usually raster the vector data to achieve a similar visual appearance instead of preserving the vector data and get a very different look.

Edit: In your example case, we can make sure that fill attributes are used instead of filters with the help of the following XSLT transformation:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@fill[ancestor::svg:symbol]" priority="1">
    <xsl:attribute name="fill">currentColor</xsl:attribute>
  </xsl:template>

  <xsl:template match="@filter[starts-with(.,'url(#colorFilter-')]">
    <xsl:attribute name="color">
      <xsl:value-of select="concat('#',substring(.,18,6))"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="svg:use[not(@filter)]">
    <xsl:copy>
      <xsl:attribute name="color">#fff</xsl:attribute>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This fully relies on how in this particular SVG the filters are named, so it's not applicable to anything else. The colors aren't quite right, though. I'd be very interested in learning why this color matrix:

0.4 0   0   0 0
0   0.6 0   0 0
0   0   0.8 0 0
0   0   0   1 0

applied to white obviously does not result in rgba(40%,60%,80%,1).

like image 70
Thomas W Avatar answered Oct 11 '22 06:10

Thomas W