0

我需要一些指导来实现以下提到的目标。

  1. 开发可以在 Windows Server 2012 上作为计划作业运行的东西,可以将 HTML 文件转换为 PDF。我们曾经使用 Omniformat,但在迁移到 Windows Server 2012 后,这不再是一个选项,因为它只会卡在那里。

  2. 我使用 iText 做了一些 POC,看起来很有希望,除了以下几点:如果样式没有指定字体系列,IE 将正确呈现它,但 IText 只会打印一个空块。这发生在我的带有阿拉伯语和俄语字符的测试用例中。

<html>
  <head></head>
  <body>
    Random Text, which I have no idea what are there.<br/>
   
    <span style="font-family: Arabic Typesetting">This works!
    Arabic : &#1587;&#1593;&#1610;&#1583;
    </span>

    <span> This isn't working
    Arabic : &#1587;&#1593;&#1610;&#1583;
    </span>
    <br/>
    <span>
    Russian: &#1057;&#1095;&#1072;&#1089;&#1090;&#1083;&#1080;&#1074;&#1099;&#1081;
    </span>
    
    <span style="font-family: MingLiu">
    Simplfied Chinese: &#24555;&#20048;
    </span>
   <br/>
    <span style="font-family: MingLiu">
    Traditional Chinese: &#24555;&#27138;
    </span>
  </body>
</html>

到目前为止我所做的。

  1. 我想我必须有一个定制的 TagProcessor。

    公共类 MyParaGraph 扩展 ParaGraph {

        @Override
        public List<Element> content(WorkerContext arg0, Tag arg1, String arg2) 
        {
              List<Element> elements = super.content(arg0, arg1,arg2);
              Iterator<Element> eleIter = elements.iterator();
              while(  eleIter.hasNext())
              {
                  Element element = eleIter.next();
                  List<Chunk> chunks = element.getChunks();
                  Iterator<Chunk> chunkIter = chunks.Iterator();
    
                  while (chunkIter.hasNext())
                  {
                       Chunk chunk = chunkIter.next();
                       for ( char charStr : arg2.toCharArray() )
                       {
                            //Intention is for future, if there is multiple Unicode characteres of different langauge, to create different chunk with differnt Font assigned.  
                            //For now, quite useless loop,
    
                             //isarabic  
                             if ( (code > 0x600 && code < code < 0x6FF) ||   (code > 0x750 && code < code < 0x77F))    
                             {  
                                  //Pardon me, my dev is not linked to internet, i cant copy and paste so i just define a sub set.
                                  if (chunk.getFont.getBaseFont()==null)
                                  {
                                      //It appear the processor failed to find the font-family style being define, as other tag with font-family define will not be null.
                                        FontFactory.register("C:/Windows/Font/Arabaric Typesetting.ttf");
                                       Font font = FontFactory.getFont("Arabaric TypeSetting");
                                       chunk.setFont(font);  //Its doesnt seems to update it to ask IText ot use Arabaric Typesetting..
                                  }
                             }        
                         }                             
                   }               
              }
        } 
    }
    

HTMLPDFConverter.java 主要:

public static void main(String[] args){
TagProcessorFactory factory = Tags.getHtmlTagProcessorFactory();
factory.addProcessor(new MyParaGraph(), "p","span", "div");

Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("D:/sample.pdf"));
writer.setPageSize(PageSize.A4);
document.open();


HtmlPipelineContext htmlContext = new HtmlPipelineContext (null);
htmlContext.setTagFactory(factory);
CssResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipleline(doc. writer)));

XMLWorker worker= new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
p.parse( new InputStreamReader(new FileInputStream("D:/sample.html"), "UTF-8");
doc.close();
}

阿拉伯文和俄文未显示在 PDF 中。感谢任何建议。对不起,我无法从开发中复制和粘贴我的 POC 代码,因为它们位于不同的网络上。谢谢

4

0 回答 0