简体   繁体   English

如何在Java / Scala中创建PDF / DOCX文件?

[英]How to create a PDF/DOCX files in Java/Scala?

I am creating a web application which will accept some inputs from user (like name, age, address etc) and generate some predefined forms with filled information for user to download and print. 我正在创建一个Web应用程序,它将接受用户的一些输入(例如姓名,年龄,地址等),并生成一些带有填充信息的预定义表单,供用户下载和打印。

For example, an Application Form for driving license or something along those lines. 例如,用于驾驶执照的申请表或类似的东西。 The backend will have the format information about the document to be generated and other information will be gathered from user from front-end. 后端将具有有关要生成的文档的格式信息,其他信息将从前端从用户那里收集。

I am going to use Play Framework 2.5 for this and Java/Scala as programming language. 我将为此使用Play Framework 2.5 ,并将Java / Scala用作编程语言。 But right now I am not aware if there are any free libraries/APIs that I can use to achieve this document generation. 但是现在我不知道是否有任何免费的库/ API可用于实现此文档生成。

I should be able to manipulate the font size, style, indentations, paragraphs, page borders, page numbers, alignments, document headers and footers, page size (A4, Legal etc) some other basic stuff. 我应该能够处理字体大小,样式,缩进,段落,页面边框,页面编号,对齐方式,文档页眉和页脚,页面大小(A4,Legal等)其他一些基本内容。 And I need documents in format that are widely supported for editing and printing purposes. 而且,我需要广泛支持用于编辑和打印目的格式的文档。 Like PDF, DOCX for example. PDF,例如DOCX DOCX is preferred so user can edit something after downloading the document before taking a print out. DOCX是首选,因此用户可以在下载文档后进行编辑,然后再打印出来。

I have used the apache POI library to parse and create ms word documents (including docx) files: 我已使用apache POI库来解析和创建ms word文档(包括docx)文件:

http://www.tutorialspoint.com/apache_poi_word/apache_poi_word_quick_guide.htm http://www.tutorialspoint.com/apache_poi_word/apache_poi_word_quick_guide.htm

It's not amazing but it's the best I've found :) 这并不奇怪,但它是我发现的最好的:)

I have used docx4j.jar which simply converts xhtml to docx. 我使用了docx4j.jar ,它只是将xhtml转换为docx。

What you can do for your requirement is save your format information as xhtml template and place input from form (like name,age,address etc) into the template at runtime. 您可以根据需要执行的操作是将格式信息保存为xhtml模板,并在运行时将来自表单的输入(例如名称,年龄,地址等)放入模板中。

This is a sample code to refer from this link 这是从此链接引用的示例代码

 public static void main(String[] args) throws Exception 
 {
        String xhtml= 
                "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%;\"><tbody><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></tbody></table>";       

        // To docx, with content controls
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

        XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

        wordMLPackage.getMainDocumentPart().getContent().addAll( 
                XHTMLImporter.convert( xhtml, null) );

        wordMLPackage.save(new java.io.File("D://sample.docx"));
}

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

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