Philipp T.
Philipp T.

Reputation: 31

PDF Creation with an SVG with curved text in PHP - no text is shown

I'm working on a project right now which requires me to render out a PDF including an SVG with curved text written on it.

The text needs to be created this way as it has to be available in multiple languages and has a variable in it.

By now, I got the PDF to be rendered out the way I want it, except for the curved text, which just gets ignored.

This is the SVG I try to render out:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="1024px" width="1280px" id="mainSVG">
    <defs>
        <path d="m250,250 a10,10 0 0 0 350,250" id="curvedTextPath"></path>
        <path d="m250,250 a10,10 0 0 1 350,250" id="curvedTextPath2"></path>
</defs>
    <circle style="fill: blue;" cx="425px" cy="375px" r="250" id="mainCircle"></circle>
    <text>
        <textPath startOffset="55%" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#curvedTextPath">Some test text that is longer</textPath>
        <textPath startOffset="22.5%" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#curvedTextPath2">Smaller text here</textPath>
    </text>
</svg>

I tried converting the SVG with Imagick, which gives me only the circle without the text:

    <?php
    $svg = file_get_contents("/path/to/test.svg");
    $image = new Imagick();
    $image->readImageBlob($svg);
    $image->setImageFormat("png");
    $image->writeImage("/path/to/lobster.png");
    ?>

Same goes for the ImageMagick command "convert" and TCPDF's "imageSVG"-function:

require_once('./tcpdf/tcpdf_import.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setCreator(PDF_CREATOR);
$pdf->setAuthor('Test');
$pdf->setTitle('Curvetest');
$pdf->setSubject('Testing of the Curve');
$pdf->addPage();
$pdf->imageSVG($file = './test.svg', $x = 15, $y = 30, $w = '', $h = '');
$filepath = '/path/to/test.pdf';
$pdf->Output($filepath, 'F');

I'm out of ideas by now, I have read that ImageMagick ignores textPath when converting, but if libRSVG is installed, it should work... well, it is, but it still doesn't work.

Upvotes: 3

Views: 1555

Answers (1)

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90285

You can use inkscape also from the command line. It will then not start a GUI even.

The following command will convert your SVG to PDF:

inkscape          \
 --without-gui    \
 --file=input.svg \
 --export-pdf out.pdf

The screenshot below is from the quoted SVG source (in the OP) after conversion to PDF:

Screenshot of out.pdf

To see more possibly useful parameters for an inkscape command, see inkscape --help.

Update

BTW, my version of ImageMagick (MacPorts version on OSX Mavericks) had no problem to convert the SVG to PDF:

$ convert -version

  Version: ImageMagick 6.9.0-0 Q16 x86_64 2014-12-06 http://www.imagemagick.org
  Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
  Features: DPC Modules
  Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib


convert input.svg out2.pdf

Here is the resulting PDF's screenshot:

Screenshot of out2.pdf

Upvotes: 2

Related Questions