我正在尝试使用itext Api将html转换为pdf,但问题是我的html也有一个元素。所以,当我看到生成的pdf图形,即元素看起来扭曲。我在paint方法中的代码如下
@Override
public void paint(RenderingContext renderingContext, ITextOutputDevice outputDevice, BlockBox blockBox) {
PdfContentByte cb = outputDevice.getWriter().getDirectContent();
float width = (float) (cssWidth / outputDevice.getDotsPerPoint());
float height =(float) (cssHeight / outputDevice.getDotsPerPoint());
System.out.println("width :"+width+" height:"+height+" outputDevice.getDotsPerPoint(): "+outputDevice.getDotsPerPoint());
PdfTemplate template = cb.createTemplate(width, height);
Graphics2D g2d = template.createGraphics(width, height);
PrintTranscoder prm = new PrintTranscoder();
TranscoderInput ti = new TranscoderInput(svg);
prm.transcode(ti, null);
PageFormat pg = new PageFormat();
Paper pp = new Paper();
pp.setSize(width, height);
pp.setImageableArea(0, 0, width, height);
pg.setPaper(pp);
prm.print(g2d,pg, 0);
g2d.dispose();
PageBox page = renderingContext.getPage();
float x = blockBox.getAbsX() + page.getMarginBorderPadding(renderingContext, CalculatedStyle.LEFT);
float y = (page.getBottom() - (blockBox.getAbsY() + cssHeight)) + page.getMarginBorderPadding(renderingContext, CalculatedStyle.BOTTOM);
System.out.println("x " + x + " y" + y + " outputDevice.getDotsPerPoint() " + outputDevice.getDotsPerPoint());
x /= outputDevice.getDotsPerPoint();
y /= outputDevice.getDotsPerPoint();
cb.addTemplate(template, x, y);
System.out.println("Done with addition " + x + " " + y);
}
现在我观察到的变化是当我更改float width = 400和height = 620然后我看到我的图表出现但顶部消失了。 有人可以帮我解决这个问题。另外我认为必须有一些高度和宽度的公式,以便图形不会扭曲。 谢谢
我想要转换的html就像这个http://tinypic.com/r/e0o4xz/8,我得到的pdf就像http://i60.tinypic.com/14sz6kp.jpg所以你可以看到我得到的pdf缺少x轴和最后一个条图表也缺失了。所以代码部分就像
float width = (float) (cssWidth / outputDevice.getDotsPerPoint());
float height = (float) (cssHeight / outputDevice.getDotsPerPoint());
所以当我评论改变cssWidth = 22000和cssHeight = 12000然后图表带有轴,最后一个条也是红色但是对齐失真。