添加自定义样式表作为转码提示的问题

时间:2019-06-12 11:08:59

标签: apache svg batik rasterizing pdf-rasterizer

我正在使用apache蜡染版1.9将SVG转换为PDF。

我需要从SVG生成PDF,并且PDF内容应放在页面的中央。

我尝试根据蜡染文档添加样式表,但使用PDFTranscoder.KEY_USER_STYLESHEET_URI时出现堆空间错误。

下面是代码和错误。谁能帮我解决这个问题?

try{
String uuid = UUID.randomUUID().toString();
String css = "svg {" + "shape-rendering: geometricPrecision;" + "text-rendering:  geometricPrecision;" + "color-rendering: optimizeQuality;" + "image-rendering: optimizeQuality;" + "}";       
cssFile = File.createTempFile("batik-default-override-", ".css");
FileUtils.writeStringToFile(cssFile, css);
FileInputStream inputStream = new FileInputStream(new File(Paths.get("Input").toAbsolutePath()+"/visualsummary.svg"));
byte[] bytes = IOUtils.toByteArray(inputStream);
FileOutputStream pdfOutputStream = new FileOutputStream(new File(Paths.get("Output").toAbsolutePath()+"/convertedSvg_"+uuid+".pdf"));
Transcoder transcoder = new PDFTranscoder();
TranscoderInput transcoderInput = new TranscoderInput(new ByteArrayInputStream(bytes));
TranscoderOutput transcoderOutput = new TranscoderOutput(pdfOutputStream);
int dpi = 300;
//String path = Paths.get("Input").toAbsolutePath()+"/customstyle.css";
//URI uri = URI.create(path);
//transcoder.addTranscodingHint(PDFTranscoder.KEY_USER_STYLESHEET_URI, uri.toString());
transcoder.addTranscodingHint(PDFTranscoder.KEY_USER_STYLESHEET_URI, cssFile.toURI().toString());
transcoder.addTranscodingHint(PDFTranscoder.KEY_HEIGHT, new Float(dpi * 42.0));
transcoder.addTranscodingHint(PDFTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,(25.4f / 72f));
transcoder.transcode(transcoderInput, transcoderOutput);
}catch(Exception e){
    e.printStackTrace();
}finally{
    cssFile.delete();
}



Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75)
    at java.awt.image.SinglePixelPackedSampleModel.createDataBuffer(SinglePixelPackedSampleModel.java:242)
    at java.awt.image.Raster.createWritableRaster(Raster.java:941)
    at org.apache.batik.ext.awt.image.rendered.AbstractRed.getData(AbstractRed.java:523)
    at org.apache.batik.ext.awt.image.GraphicsUtil.drawImage(GraphicsUtil.java:264)
    at org.apache.batik.ext.awt.image.GraphicsUtil.drawImage(GraphicsUtil.java:455)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:549)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.CanvasGraphicsNode.primitivePaint(CanvasGraphicsNode.java:159)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at org.apache.batik.gvt.CompositeGraphicsNode.primitivePaint(CompositeGraphicsNode.java:165)
    at org.apache.batik.gvt.AbstractGraphicsNode.paint(AbstractGraphicsNode.java:509)
    at org.apache.fop.svg.PDFTranscoder.transcode(PDFTranscoder.java:185)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
    at com.aramco.ecc.drilleye.ManipulatePDF.main(ManipulatePDF.java:69)

0 个答案:

没有答案