javapdfsvgbatik

Batik SVG to PDF - PDFTranscoder - Page Size A4


I was succesful converting an SVG File into PDF using Apache Batik.

The following code is used to generate PDF:

import org.apache.fop.svg.PDFTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
...

File svgFile = new File("./target/test.svg");
...

PDFTranscoder transcoder = new PDFTranscoder();
try (FileInputStream fileInputStream = new FileInputStream(svgFile); FileOutputStream fileOutputStream = new FileOutputStream(new File("./target/test-batik.pdf"))) {
    TranscoderInput transcoderInput = new TranscoderInput(fileInputStream);
    TranscoderOutput transcoderOutput = new TranscoderOutput(fileOutputStream);
    transcoder.transcode(transcoderInput, transcoderOutput);
}

Now I want to influence the page size of the resulting PDF so I get a page size for A4. How could I do that?

I have tried some key hints but with no effect.


Solution

  • I've recently had this same problem. This might not exactly solve your issue, but I was at least able to produce a PDF with the correct aspect ratio for a page (U.S. letter size in this case) with the following Groovy (almost Java) code:

    ...
    
    TranscoderInput transcoderInput = new TranscoderInput(fileInputStream)
    TranscoderOutput transcoderOutput = new TranscoderOutput(fileOutputStream)
    PDFTranscoder transcoder = new PDFTranscoder()
    int dpi = 100
    transcoder.addTranscodingHint(PDFTranscoder.KEY_WIDTH, dpi * 8.5 as Float)
    transcoder.addTranscodingHint(PDFTranscoder.KEY_HEIGHT, dpi * 11 as Float)
    transcoder.transcode(transcoderInput, transcoderOutput)
    

    Hope this helps.