我需要一些指导来实现以下提到的目标。
开发可以在 Windows Server 2012 上作为计划作业运行的东西,可以将 HTML 文件转换为 PDF。我们曾经使用 Omniformat,但在迁移到 Windows Server 2012 后,这不再是一个选项,因为它只会卡在那里。
我使用 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 : سعيد
</span>
<span> This isn't working
Arabic : سعيد
</span>
<br/>
<span>
Russian: Счастливый
</span>
<span style="font-family: MingLiu">
Simplfied Chinese: 快乐
</span>
<br/>
<span style="font-family: MingLiu">
Traditional Chinese: 快樂
</span>
</body>
</html>
到目前为止我所做的。
我想我必须有一个定制的 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 代码,因为它们位于不同的网络上。谢谢