简体   繁体   English

使用iText和org.w3c.dom.Document旋转PDF

[英]Rotate PDFs using iText and org.w3c.dom.Document

i'm working with iText in Java Servlets. 我正在使用Java Servlet中的iText。 I'm fighting with the creation of PDFs from my JSP files. 我正在努力从我的JSP文件创建PDF。 I've seen that is not possible, so i created new servlets which parse a String (my HTML page). 我已经看到这是不可能的,所以我创建了新的servlet,用于解析String(我的HTML页面)。 Here part of the code: 这里是部分代码:

StringBuffer buffer = getHTMLinBuffer(consulenti, anUser);
DocumentBuilder builder = DocumentBuilderFactory
    .newInstance().newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(buffer.toString().getBytes("UTF-8")));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
String fileName = "listaConsulenti.pdf";
String absolutePath = getServletContext().getRealPath("/");
String tempPath = absolutePath + "temp/";
File file = new File(tempPath, fileName);
file.createNewFile();
OutputStream os = new FileOutputStream(file);
renderer.layout();
renderer.createPDF(os);
os.close();

This code works. 此代码有效。 Now i have to create another PDF which needs to have an A4 page rotated by 90°. 现在,我必须创建另一个PDF,该文件需要将A4页旋转90°。 Using org.w3c.dom.Document i can't find how to do. 我找不到使用org.w3c.dom.Document的方法。 There is the possibility to use another Document Class, the com.itextpdf.text.Document which has the rotate() method to rotate it, but using this Document i can't find how to do to parse my String (HTML code)... 可以使用另一个Document类,即具有rotate()方法来旋转它的com.itextpdf.text.Document ,但是使用此Document我找不到如何解析我的String(HTML代码)。 ..

Hints? 提示?

如果您需要的只是侧面的文档,则可以使用以下方法:

private Document document = new Document(PageSize.A4.rotate());

...i've just found out, that iTextRendere is not part of iText, but of Flying Source. ...我刚刚发现,iTextRendere不是iText的一部分,而是Flying Source的一部分。 In this link i've seen that the creation of a landscape page is simply done by adding some css to the source (X)HTML: 此链接中,我已经看到,只需在源(X)HTML中添加一些CSS即可轻松完成横向页面的创建:

<style type="text/css"> 
@page{ size: 11.69in 8.27in;}
...
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM