我需要一些关于实现下述目标的指导。
开发可在Windows Server 2012上作为预定作业运行的可将HTML文件转换为PDF的内容。我们曾经使用Omniformat,但在迁移到Windows Server 2012之后,这已不再是一个选项,因为它只会停留在那里。
我使用iText做了一些POC,看起来很有希望,除了以下几点: 一世。如果样式没有指定font-family,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>
&#13;
到目前为止我做了什么。
我认为我必须有一个自定义的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代码,因为它们位于不同的网络上。 感谢