SVG to PDF on a shared linux server
I stumbled across TCPDF today which would have been perfect for this, had I known about it at the time. It's just a collection of pure PHP classes, no external dependencies for most things.
It can build PDF's from scratch and you can include pretty much anything you want in there, including SVG (amongst many, many other things), as shown in these examples:
http://www.tcpdf.org/examples.php
Main project page is here:
http://www.tcpdf.org/
Sourceforge page is here:
http://sourceforge.net/projects/tcpdf/
batik: svg to pdf on linux (no X server)
Solved by following the recipe here:
http://batik.2283329.n4.nabble.com/Placing-SVG-Text-into-PDF-td3778127.html
major steps:
- compile fop with ant all
- copy fop-transcoder-allinone.jar under name pdf-transcoder.jar into the classpath
- copy xmlgraphics-commons-1.4.jar from fop's lib directory into the classpath
How can I programatically convert SVG files containing text to PDF files (specifically on CentOS 5.3 x86_64)?
You could try princexml, it's free for non-commercial use.
Convert SVG to PDF
Thanks to Adrian for showing how the Batik rasterizer API is supposed to be used. However, I needed a more lightweight solution--- I can't write to temporary files, and I want fewer dependencies. So, starting from the methods he pointed to, I found a way to access the lower-level code to do the conversion and nothing else.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.fop.svg.PDFTranscoder;
public class Test {
public static void main(String[] argv) throws TranscoderException, FileNotFoundException {
Transcoder transcoder = new PDFTranscoder();
TranscoderInput transcoderInput = new TranscoderInput(new FileInputStream(new File("/tmp/test.svg")));
TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File("/tmp/test.pdf")));
transcoder.transcode(transcoderInput, transcoderOutput);
}
}
The compile-and-run commands are
javac -cp batik-rasterizer.jar -d build Test.java
java -cp build:batik-rasterizer.jar Test
The important point is that TranscoderInput
and TranscoderOutput
can work with any InputStream
and OutputStream
, not just file streams. Note that one of the constructors takes a org.w3c.dom.Document
, which means that you don't even need to serialize an SVG DOM into an SVG string, saving an additional step.
This version also doesn't write anything to stdout/stderr, unlike the high-level API.
For JPEG, PNG, or TIFF output, replace org.apache.fop.svg.PDFTranscoder
with org.apache.batik.transcoder.image.JPEGTranscoder
, PNGTranscoder
, or TIFFTranscoder
(note that these raster formats are in a different package).
(I'm not quite sure how Java finds the org.apache.batk.transcoder.*
and org.apache.fop.svg.PDFTranscoder
classes, since I don't see them in the batik-rasterizer.jar
.)
Edit:
Although the simple commandline-compilation works with the batik-rasterizer.jar
only, it's doing some sort of classloader magic to find all the necessary classes. In a more realistic case (building a project with Ant), you have to find the classes by hand. They can be found in batik-1.7.zip
from the Batik project and fop-1.1.zip
from the FOP project. From Batik, you need to compile with batik-transcoder.jar
and run with
batik-transcoder.jar
batik-anim.jar
batik-awt-util.jar
batik-bridge.jar
batik-css.jar
batik-dom.jar
batik-ext.jar
batik-gvt.jar
batik-parser.jar
batik-script.jar
batik-svg-dom.jar
batik-util.jar
batik-xml.jar
xml-apis-ext.jar
From FOP, you need to compile with fop.jar
and run with
fop.jar
avalon-framework-4.2.0.jar
xmlgraphics-commons-1.5.jar
Export SVG elements to PDF?
I do not know of any strong PDF libraries on the client side.
A quick possible way would be to send the svg content to a server, and use something like batik for java to turn the svg to pdf and then send the response to the client again.
Here is a related SO for the converstion.
Generating PDFs from SVG input
Have you considered svglib?
It looks quite promising, especially as reportlab is the featured pdf tool in Django's docs.
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
drawing = svg2rlg("file.svg")
renderPDF.drawToFile(drawing, "file.pdf")
Related Topics
How to Diff Top Lines of Two Files Without Intermediate File
Injecting Code into Executable at Runtime
How to Echo a Sum of a Variable and a Number
How to Find Which Process Is Leaking Memory
Auto Exit Telnet Command Back to Prompt Without Human Intervention ^] Quit Close Exit Code 1
Strange Return Value "134" to Call Gawk in a Bash Script
How to Use Cx_Freeze in Linux to Create a Package to Be Used in Windows
Installing Docker on Centos 6.6
Best Way to Overwrite File with Itself
How Many Packets or Bytes Are in the Socket Receive Queue
Pass Private Key Password to Openvpn Command Directly in Ubuntu 10.10
Running R Scripts or Commands with Interpretor in Unix for Unix-Layman
Lowest Latency Notification Method Between Process Under Linux
Run Docker in Ubuntu Live Disk
How to Make Sure the Floating Point Arithmetic Result the Same in Both Linux and Windows
Calculating Rounded Percentage in Shell Script Without Using "Bc"