Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XMLWorkerHelper性能缓慢_Java_Html_Pdf_Tags_Itext - Fatal编程技术网

Java XMLWorkerHelper性能缓慢

Java XMLWorkerHelper性能缓慢,java,html,pdf,tags,itext,Java,Html,Pdf,Tags,Itext,我正在使用java中的itext 5.3生成PDF。我使用HTMLWorker.parsetList(Reader,StyleSheet)将部分转换为字符串,其中包含HTML标记,如粗体、斜体、href等,并转换为PDF。我不想生成完整的HTML到PDF,而是PDF中的一部分文本将是HTML。例如,“This is test bold text”之类的字符串将部分文本转换为粗体 使用HTMLWorker时性能良好 由于它现在已被弃用,我开始使用xmlworkerhelp.parseXHtml(E

我正在使用java中的itext 5.3生成PDF。我使用
HTMLWorker.parsetList(Reader,StyleSheet)
将部分转换为字符串,其中包含HTML标记,如粗体、斜体、href等,并转换为PDF。我不想生成完整的HTML到PDF,而是PDF中的一部分文本将是HTML。例如,“This is test bold text”之类的字符串将部分文本转换为粗体

使用
HTMLWorker
时性能良好

由于它现在已被弃用,我开始使用
xmlworkerhelp.parseXHtml(ElementHandler,Reader)
,我发现与
HTMLWorker
相比,它的性能非常差

如果有人对解决方案或任何其他解决方法有任何想法,请让我知道

下面是示例代码,其他带有示例代码的帖子位于

公共类HTMLElementHandler实现ElementHandler{
私人短语;
专用字体;
专用HTMLElementHandler(短语、字体){
超级();
固定短语(短语);
setFont(字体);
}
@凌驾
公共无效添加(可写){
if(可写元素的可写实例){
列表元素=((WritableElement)writable.elements();
用于(元素元素:元素){
List chunks=elem.getChunks();
for(块:块){
Font chunkFont=chunk.getFont();
//在这里使用字体
}
短语.setFont(字体);
短语.添加(elem);
}
}
}
公共短语getPhrase(){
返回这个短语;
}
公共无效设置短语(短语){
这个短语=短语;
}   
公共字体getFont(){
返回此.font;
}    
公共void setFont(字体){
this.font=font;
}
} 
另一个javafile.java

Phrase ph = new Phrase();
Font font = FontFactory.getFont(FontFactory.getFont("Arial").getFamilyname(), 12, new BaseColor(0, 102, 153));
XMLWorkerHelper.getInstance().parseXHtml(new HTMLElementHandler(phrase, font), "This is test <bold> bold </bold> text");
短语ph=新短语();
Font Font=FontFactory.getFont(FontFactory.getFont(“Arial”).getFamilyname(),12,新的基色(0,102,153));
XMLWorkerHelper.getInstance().parseXHtml(新的HTMLElementHandler(短语,字体),“这是测试粗体文本”);

此问题的原因是在有效解析(X)HTML之前注册字体目录,这是操作的一部分。这要花很多时间

这可以通过提供一个不会查找任何字体的字体提供程序来避免,即不会注册任何字体目录。此字体提供程序可以通过以下方式创建:

new XMLWorkerFontProvider( XMLWorkerFontProvider.DONTLOOKFORFONTS )
您可以将此字体提供程序作为参数提供给
XMLWorkerHelper.getInstance().parseXHtml(…)
,但是如果您的第一个参数是
ElementHandler
,则不能。我真的不知道为什么,我只是偶尔使用iText

我将给出(X)HTML为字符串的示例:

File tempPdfFile = File.createTempFile( "temp_pdf_", ".pdf" );
tempPdfFile.deleteOnExit( );

try( OutputStream os = new FileOutputStream( tempPdfFile ) )
{
    Document pdfDocument = new Document( PageSize.A4 );
    PdfWriter pdfWriter = PdfWriter.getInstance( pdfDocument, os );
    pdfDocument.open( );

    String htmlText = getHtmlText( ); // your method that returns HTML as text

    XMLWorkerHelper.getInstance( ).parseXHtml ( 
        pdfWriter,
        pdfDocument,
        new ByteArrayInputStream( htmlText.getBytes( StandardCharsets.UTF_8 ) ),
        StandardCharsets.UTF_8,
        new XMLWorkerFontProvider( XMLWorkerFontProvider.DONTLOOKFORFONTS )
    );

    pdfDocument.close( );
    pdfWriter.close( );
}

Desktop.getDesktop( ).open( tempPdfFile );

不幸的是,我也从同一个库的C#端口iTextSharp上看到了这个类的一些相当糟糕的性能,尽管不是Java。Culfrate是ParseXHtml内部的以下方法:iTextSharp.text.FontFactoryImp.RegisterDirectories另请参见iTextSharp问题-
File tempPdfFile = File.createTempFile( "temp_pdf_", ".pdf" );
tempPdfFile.deleteOnExit( );

try( OutputStream os = new FileOutputStream( tempPdfFile ) )
{
    Document pdfDocument = new Document( PageSize.A4 );
    PdfWriter pdfWriter = PdfWriter.getInstance( pdfDocument, os );
    pdfDocument.open( );

    String htmlText = getHtmlText( ); // your method that returns HTML as text

    XMLWorkerHelper.getInstance( ).parseXHtml ( 
        pdfWriter,
        pdfDocument,
        new ByteArrayInputStream( htmlText.getBytes( StandardCharsets.UTF_8 ) ),
        StandardCharsets.UTF_8,
        new XMLWorkerFontProvider( XMLWorkerFontProvider.DONTLOOKFORFONTS )
    );

    pdfDocument.close( );
    pdfWriter.close( );
}

Desktop.getDesktop( ).open( tempPdfFile );