2015-10-13 76 views
0

我想从HTML字符串创建PDF表格。我可以创建该表格,但不是文字,我收到了问号。这里是我的代码:使用UTF-8填充的HTML字符串创建PDF表格

public class ExportReportsToPdf implements StreamSource { 
private static final long serialVersionUID = 1L; 

private ByteArrayOutputStream byteArrayOutputStream; 

public static final String FILE_LOC = "C:/Users/KiKo/CasesWorkspace/case/Export.pdf"; 

private static final String CSS = "" 
     + "table {text-align:center; margin-top:20px; border-collapse:collapse; border-spacing:0; border-width:1px;}" 
     + "th {font-size:14px; font-weight:normal; padding:10px; border-style:solid; overflow:hidden; word-break:normal;}" 
     + "td {padding:10px; border-style:solid; overflow:hidden; word-break:normal;}" 
     + "table-header {font-weight:bold; background-color:#EAEAEA; color:#000000;}"; 

public void createReportPdf(String tableHtml, Integer type) throws IOException, DocumentException { 

    // step 1 
    Document document = new Document(PageSize.A4, 20, 20, 50, 20); 

    // step 2 
    PdfWriter.getInstance(document, new FileOutputStream(FILE_LOC)); 

    // step 3 
    byteArrayOutputStream = new ByteArrayOutputStream(); 
    PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream); 
    if (type != null) { 
     writer.setPageEvent(new Watermark()); 
    } 

    // step 4 
    document.open(); 

    // step 5 
    document.add(getTable(tableHtml)); 

    // step 6 
    document.close(); 
} 

private PdfPTable getTable(String tableHtml) throws IOException { 

    // CSS 
    CSSResolver cssResolver = new StyleAttrCSSResolver(); 
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes())); 
    cssResolver.addCss(cssFile); 

    // HTML 
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); 
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); 

    // Pipelines 
    ElementList elements = new ElementList(); 
    ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null); 
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); 
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); 

    // XML Worker 
    XMLWorker worker = new XMLWorker(css, true); 
    XMLParser parser = new XMLParser(worker); 

    InputStream inputStream = new byteArrayInputStream(tableHtml.getBytes()); 
    parser.parse(inputStream); 

    return (PdfPTable) elements.get(0); 
} 

private static class Watermark extends PdfPageEventHelper { 

    @Override 
    public void onEndPage(PdfWriter writer, Document document) { 
     try { 
      URL url = Thread.currentThread().getContextClassLoader().getResource("/images/memotemp.jpg"); 
      Image background = Image.getInstance(url); 
      float width = document.getPageSize().getWidth(); 
      float height = document.getPageSize().getHeight(); 
      writer.getDirectContentUnder().addImage(background, width, 0, 0, height, 0, 0); 
     } catch (DocumentException | IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

@Override 
public InputStream getStream() { 
    return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); 
} 

}

此代码工作,并且我得到这样的: Not Good

我尝试添加UTF-8,

InputStream inputStream = new byteArrayInputStream(tableHtml.getBytes("UTF-8")); 

但比我得到这个: Not good(utf8)

我想是这样的:

Good

我认为这个问题是与编码,但我不知道如何解决这个bug。有什么建议么...?

回答

0

要从某个编码中的(Unicode)字符串获取字节,请指定它,否则使用默认系统编码。

tableHtml.getBytes(StandardCharsets.UTF_8) 

在你的情况下,“Windows-1251”似乎更好的匹配,因为PDF似乎不使用UTF-8。

也许原始tableHTML字符串被错误的编码读取。可能检查,如果它来自文件或数据库。

+0

转换内存中的字节将无助于输出。 – markbernard

+0

我看到它已经尝试过。尽管这也必须完成。 –

0

您需要通过创建BaseFont类的实例来告诉iText使用什么编码。然后在您的document.add(getTable(tableHtml));中,您可以添加对字体的调用。例如在http://itextpdf.com/examples/iia.php?id=199

我不能告诉你如何创建一个表,但是阶级PdfPTable有一个方法addCell(PdfCell)和一个构造函数PdfCell需要PhrasePhrase可以用StringFont来构建。字体类以BaseFont作为构造函数参数。

如果您环顾Javadoc for iText,您会看到各种类将Font作为构造函数参数。

+0

嗯,我不理解你。你能修改我的代码并帮助我吗? – KiKo

+0

我更新了我的答案。 – markbernard