如何使用 itext 5.1.1 从 html 输出以横向模式生成 pdf
How to generate a pdf in landscape mode from a html output using itext 5.1.1
我们计划通过传递 html 文本作为输入以横向模式生成 pdf。此外,pdf 是在横向模式下生成的,但内容是纵向模式。
以下是我们用来执行上述要求的步骤。
1) 从源系统接收xml内容。
2) 使用 xsl 从 xml.
生成 html 输出
3) 将 html 输出作为输入传递给 pdf java class,进一步 class 将以横向模式生成 pdf。
示例xml 文件:http://www.filedropper.com/resettsample
示例 xsl 文件:http://www.filedropper.com/resettcopy
java代码
package testproject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class PDFTest {
public static void main(String[] args) {
try {
new PDFTest().PDFCreateRequest();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void PDFCreateRequest() throws Exception {
StringWriter stringWriter = null;
Result result = null;
TransformerFactory tFactory = null;
Transformer transformer = null;
String strXMLFile = null;
String strXSLFile = null;
String strHTMLString = null;
String strFeedFolderPath = null;
String strPDFFileName = null;
String strPDFFileNameWithExtension = null;
String strPDFFile = null;
Document document = null;
PdfWriter pdfWriter = null;
strXMLFile = "C:\RESETT_sample.xml";
String timeStamp =
new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
strXSLFile = "C:\RESETT_Copy.xsl";
stringWriter = new StringWriter();
result = new StreamResult(stringWriter);
tFactory = TransformerFactory.newInstance();
transformer = tFactory.newTransformer(new StreamSource(strXSLFile));
transformer.transform(new StreamSource(strXMLFile), result);
strHTMLString = stringWriter.toString();
System.out.println("###### [HTML output " + strHTMLString);
strPDFFileName = "RESETT_" + timeStamp;
strFeedFolderPath = "C:\pdfdocuments";
strPDFFileNameWithExtension = strPDFFileName + ".pdf";
strPDFFile =
strFeedFolderPath + File.separator + strPDFFileNameWithExtension;
// This is for PDF Landscape output
document =
new Document(new RectangleReadOnly(842, 595), 10, 10, 75, 20);
// This is for PDF Portrait output
document = new Document(PageSize.A4, 10, 10, 75, 20);
pdfWriter =
PdfWriter.getInstance(document, new FileOutputStream(strPDFFile));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document,
new StringReader(strHTMLString));
document.close();
}
}
如有任何关于如何实现所需输出的建议,我们将不胜感激。
谢谢
桑迪普
转到 XML Worker examples 并采用您想要的任何示例,但更改以下行:
Document document = new Document();
变成这样:
Document document = new Document(PageSize.A4.rotate());
这将导致文档为横向而非纵向。
您可能需要升级您的 iText/XML Worker 版本(确保您始终使用对应的 iText 和 XML Worker 版本)。
我们计划通过传递 html 文本作为输入以横向模式生成 pdf。此外,pdf 是在横向模式下生成的,但内容是纵向模式。 以下是我们用来执行上述要求的步骤。
1) 从源系统接收xml内容。
2) 使用 xsl 从 xml.
生成 html 输出3) 将 html 输出作为输入传递给 pdf java class,进一步 class 将以横向模式生成 pdf。
示例xml 文件:http://www.filedropper.com/resettsample
示例 xsl 文件:http://www.filedropper.com/resettcopy
java代码
package testproject; import java.io.File; import java.io.FileOutputStream; import java.io.StringReader; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.RectangleReadOnly; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class PDFTest { public static void main(String[] args) { try { new PDFTest().PDFCreateRequest(); } catch (Exception ex) { ex.printStackTrace(); } } public static void PDFCreateRequest() throws Exception { StringWriter stringWriter = null; Result result = null; TransformerFactory tFactory = null; Transformer transformer = null; String strXMLFile = null; String strXSLFile = null; String strHTMLString = null; String strFeedFolderPath = null; String strPDFFileName = null; String strPDFFileNameWithExtension = null; String strPDFFile = null; Document document = null; PdfWriter pdfWriter = null; strXMLFile = "C:\RESETT_sample.xml"; String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()); strXSLFile = "C:\RESETT_Copy.xsl"; stringWriter = new StringWriter(); result = new StreamResult(stringWriter); tFactory = TransformerFactory.newInstance(); transformer = tFactory.newTransformer(new StreamSource(strXSLFile)); transformer.transform(new StreamSource(strXMLFile), result); strHTMLString = stringWriter.toString(); System.out.println("###### [HTML output " + strHTMLString); strPDFFileName = "RESETT_" + timeStamp; strFeedFolderPath = "C:\pdfdocuments"; strPDFFileNameWithExtension = strPDFFileName + ".pdf"; strPDFFile = strFeedFolderPath + File.separator + strPDFFileNameWithExtension; // This is for PDF Landscape output document = new Document(new RectangleReadOnly(842, 595), 10, 10, 75, 20); // This is for PDF Portrait output document = new Document(PageSize.A4, 10, 10, 75, 20); pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(strPDFFile)); document.open(); XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, new StringReader(strHTMLString)); document.close(); } }
如有任何关于如何实现所需输出的建议,我们将不胜感激。
谢谢 桑迪普
转到 XML Worker examples 并采用您想要的任何示例,但更改以下行:
Document document = new Document();
变成这样:
Document document = new Document(PageSize.A4.rotate());
这将导致文档为横向而非纵向。
您可能需要升级您的 iText/XML Worker 版本(确保您始终使用对应的 iText 和 XML Worker 版本)。