我有3个SVG文件,我想将其转换为单个PDF。 我正在尝试用Apache FOP做这个,但我到目前为止还不成功。 我正在努力调整this FOP example。
我的想法是将所有3个SVG文件一个接一个地写入生成的PDF中,但这不起作用。我的代码如下:
public void convertSVG2PDF(File svg, File svg2, File svg3, File pdf) throws IOException,
TranscoderException {
// Create transcoder
Transcoder transcoder = new PDFTranscoder();
// Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();
// Setup input
InputStream in = new java.io.FileInputStream(svg);
InputStream in2 = new java.io.FileInputStream(svg2);
InputStream in3 = new java.io.FileInputStream(svg3);
try {
TranscoderInput input = new TranscoderInput(in);
TranscoderInput input2 = new TranscoderInput(in2);
TranscoderInput input3 = new TranscoderInput(in3);
// Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
out = new java.io.BufferedOutputStream(out);
try {
TranscoderOutput output = new TranscoderOutput(out);
TranscoderOutput output2 = new TranscoderOutput(out);
TranscoderOutput output3 = new TranscoderOutput(out);
// Do the transformation
transcoder.transcode(input, output);
transcoder.transcode(input2, output2);
transcoder.transcode(input3, output3);
} finally {
out.close();
}
} finally {
in.close();
}
}
最后我的SVG图像看起来像这样,它们是从javascript应用程序生成的。
< svg height="70" id="topBar" version="1.1" width="1060" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip)" height="560" transform="translate(60,0)" width="1060">
<clipPath id="clip">
<rect height="70" width="1000"/>
</clipPath>
<rect fill="white" fill-opacity="1" height="70" id="rect" stroke="black" stroke-width="2" width="1000" x="0" y="0"/>
<path d="M-35.63129521110456,34L-1.6312952111045584,68L32.36870478889544,34L-1.6312952111045584,1Z" fill="white" id="mileStones" stroke="green" stroke-width="2"/>
<path d="M269.4286615414018,34L303.4286615414018,68L337.4286615414018,34L303.4286615414018,1Z" fill="white" id="mileStones" stroke="green" stroke-width="2"/>
<path d="M674.5082762455497,34L708.5082762455497,68L742.5082762455497,34L708.5082762455497,1Z" fill="white" id="mileStones" stroke="green" stroke-width="2"/>
<text id="mileStoneText" style="font-size: 15px;" x="-16.631295211104558" y="40">MS1</text>
<text id="mileStoneText" style="font-size: 15px;" x="288.4286615414018" y="40">MS2</text>
<text id="mileStoneText" style="font-size: 15px;" x="693.5082762455497" y="40">MS3</text>
</g>
到目前为止,我的代码覆盖了pdf 2次,结果PDF只包含最后一个SVG。我也可以用里面的图像创建3个单独的PDF。但我需要一个页面上的所有3个在另一个页面之下。
答案 0 :(得分:0)
您可以使用PDFMergerUtility合并PDF。以下是一些基于您的代码的示例代码:
public class SvgToPdfConverter {
private ArrayList<byte[]> pdfs = new ArrayList<byte[]>();
public void convertSVG2PDF(File svg) throws IOException, TranscoderException {
// Create transcoder
Transcoder transcoder = new PDFTranscoder();
// Input
FileInputStream bais = new FileInputStream(svg);
// Output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TranscoderInput input = new TranscoderInput(bais);
TranscoderOutput output = new TranscoderOutput(baos);
// Do the transformation
transcoder.transcode(input, output);
pdfs.add(baos.toByteArray());
}
public void merge(File mergedPDF) throws IOException, COSVisitorException {
PDFMergerUtility ut = new PDFMergerUtility();
for (byte[] bytes : pdfs) {
ut.addSource(new ByteArrayInputStream(bytes));
}
ut.setDestinationStream(new FileOutputStream(mergedPDF));
ut.mergeDocuments();
}
}
主要方法的一个例子:
public static void main(String[] args) {
File exampleSVGFile = new File(System.getProperty("user.home") + File.separator + "example.svg");
File exampleOutputFile = new File(System.getProperty("user.home") + File.separator + "example.pdf");
SvgToPdfConverter converter = new SvgToPdfConverter();
try {
converter.convertSVG2PDF(exampleSVGFile);
converter.convertSVG2PDF(exampleSVGFile);
converter.convertSVG2PDF(exampleSVGFile);
} catch (Exception e) {
log.error("Converting svg failed. ", e);
}
try {
converter.merge(exampleOutputFile);
} catch (Exception e) {
log.error("Merge failed. ", e);
}
}