首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Batik SVG to PDF - PDFTranscoder -页面大小A4

Batik SVG to PDF - PDFTranscoder -页面大小A4
EN

Stack Overflow用户
提问于 2016-12-27 23:22:33
回答 1查看 2.1K关注 0票数 8

我使用Apache Batik成功地将SVG文件转换为PDF。

以下代码用于生成PDF:

代码语言:javascript
复制
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);
}

现在我想影响生成的PDF的页面大小,所以我得到了一个A4的页面大小。我怎么能这么做呢?

我已经尝试了一些关键提示,但没有效果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 05:35:01

我最近也遇到了同样的问题。这可能不能完全解决您的问题,但我至少能够使用以下Groovy (几乎是Java)代码为页面(本例中为美国字母大小)生成具有正确纵横比的PDF:

代码语言:javascript
复制
...

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)

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41347776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档