简体   繁体   English

如何使用飞碟将大表格转换为 PDF?

[英]How To convert a large table to PDF using flying saucer?

I have a html with large number of columns(you can find the sample at this link )我有一个包含大量列的 html(您可以在此链接中找到示例)

Now When I try to convert it to PDF using flying saucer ( jar link recompiled to work with iText 2.1.X), the generated PDF has truncated Columns现在,当我尝试使用飞碟将其转换为 PDF 时(重新编译的jar 链接可与 iText 2.1.X 一起使用),生成的 PDF已截断列

Is there some way to make Flying saucer to either break the table or to increase the width of the page according to the html content?有什么方法可以让飞碟根据html内容要么打破表格要么增加页面宽度?

This is the code that I am using这是我正在使用的代码

String doc = file.toURI().toURL().toString();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc);
String outputFile = "test.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();

Where file is the html which I am trying to convert.其中file是我要转换的 html。

Use YAHP library.This is the best library I have worked so far to convert HTML to PDF.使用YAHP库。这是我迄今为止将 HTML 转换为 PDF 的最佳库。 This is written on the top of flying saucer which is a big disappointment as compared to it's popularity.It won't even render simple input text boxes.So, I turned to YAHP library which is excellent for your case.这是写在飞碟的顶部,与它的受欢迎程度相比,这是一个很大的失望。它甚至不会呈现简单的输入文本框。所以,我转向了非常适合您的情况的YAHP库。

try this code after you get all the jars related to this library.在获得与此库相关的所有 jar 后,尝试使用此代码。

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.allcolor.yahp.converter.CYaHPConverter;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyHtmlSerializer;
import org.htmlcleaner.TagNode;

public class YahpHtmlToPdf {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
        try{
            CleanerProperties props = new CleanerProperties();
            props.setTranslateSpecialEntities(true);
            props.setTransResCharsToNCR(true);
            props.setOmitComments(true);
            TagNode tagNode = new HtmlCleaner(props).clean(new File("C:\\Users\\MyComputer\\Desktop\\aspose.html"));
            String newString=new PrettyHtmlSerializer(props).getAsString(tagNode, "ISO-8859-1");
            CYaHPConverter converter = new CYaHPConverter();
            File fout = new File("C:\\sample\\aspose.pdf");
            FileOutputStream out = new FileOutputStream(fout);
            Map properties = new HashMap();
            List headerFooterList = new ArrayList();
            properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
            converter.convertToPdf(newString,IHtmlToPdfTransformer.A1P,headerFooterList, "file:///temp/",out,properties);
            out.flush();
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }   

}

在此处输入图片说明 This is screenshot of pdf generated with your html.这是用您的 html 生成的 pdf 的屏幕截图。 .You can specify page size like this IHtmlToPdfTransformer.A1P . .您可以像这样指定页面大小IHtmlToPdfTransformer.A1P

Its possible to change the pagesize if you know the size you need.如果您知道所需的大小,则可以更改页面大小。

Use the @page rule for this:为此使用@page规则:
Please note: in this example im working on html with Jsoup (see comment).请注意:在这个例子中,我使用Jsoup处理 html(见评论)。

/*
 * This part is optional - Jsoup is used for cleaning the html and inserting the style tag into the head.
 * You can use everything else for doing this.
 * 
 * If you will use Jsoup, make shure you set proper charset (2nd parameter).
 * 
 * Note: this is NOT a W3C Document but a Jsoup one.
 */
Document doc = Jsoup.parse(file, null);

/*
 * Here you specify the pagesize you need (size: with height).
 * Inserting this html is the key part!
 */
doc.head().append("<style type=\"text/css\"><!--@page { size:50.0cm 20.0cm; }--></style>");


ITextRenderer renderer = new ITextRenderer();

/*
 * This part ist jsoup related. 'doc.toString()' does nothing else than
 * returning the Html of 'doc' as a string.
 * 
 * You can set it like in your code too.
 */
renderer.setDocumentFromString(doc.toString());

final String outputFile = "test.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();

With this code you'll get a Pdf where the whole table is on a landscape page (maybe you have to change width / height for your needs.使用此代码,您将获得一个 Pdf,其中整个表格都位于横向页面上(也许您必须根据需要更改宽度/高度。

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

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