我添加了一个数据导出器。如果数据网格有阿拉伯字母,则它们根本没有被部署,或者它们被显示为不相交且彼此重叠。这是一张图片:
为了解决这个问题,我添加了PDFOption,包括一个新的
Arial
字体。
pdfOpt = new PDFOptions();
InputStream fontStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/fonts/arial.ttf");
BaseFont baseFont = null;
try {
baseFont = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontStream.readAllBytes(), null);
} catch (IOException ex) {
Logger.getLogger(PrimefacesPDFExporterOptions.class.getName()).log(Level.SEVERE, null, ex);
} catch (DocumentException ex) {
Logger.getLogger(PrimefacesPDFExporterOptions.class.getName()).log(Level.SEVERE, null, ex);
}
Font font = new Font(baseFont, 12, Font.NORMAL);
pdfOpt.setFontName(font.getBaseFont().getFullFontName()[0][3]);
此解决方案的问题是我无法自行设置字体,只能设置名称。所以,我需要设置编码。为此,我将编码属性添加到
p:dataExporter
并将值设置为 Identity-H
:
<p:dataExporter options="#{primefacesPDFExporterOptions.pdfOpt}" encoding="Identity-H" type="pdf" preProcessor="#{primefacesPDFExporterOptions.makePDFLandscape}" pageOnly="false" target="explorerResultsTableId" fileName="data"/>
这并没有解决问题!
我尝试应用的另一个选项是将 PDF 中每个单元格的字体更改为创建的字体。但我无法获得细胞!
这是
PF 12
的基本解决方案,完成工作,几乎没有变化,但可以改进。我在exporter
中定义了一个自定义的xhtml
,像这样:
<p:commandButton value="PDF">
<p:dataExporter type="pdf" target="cars" fileName="cars" exporter="#{dataGridView.exporter}"/>
</p:commandButton>
我使用与阿拉伯字符兼容的字体,如
ARIALUNI.TTF
,我将文件嵌入到项目中(它很容易在互联网上找到)。exporter
:
public class DataGridView implements Serializable {
...
@PostConstruct
public void init() {
...
exporter = new MyPDFExporter();
}
...
}
MyPDFExporter
很可能是PF版本(DataTableExporter
),我只改变了applyFont
功能,使用嵌入式(我不能直接让它工作,通过选项),所以它看起来像这样
protected void applyFont(String fontName, String encoding) {
InputStream fontStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/fonts/ARIALUNI.TTF");
BaseFont baseFont = null;
try {
baseFont = BaseFont.createFont("ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontStream.readAllBytes(), null);
} catch (DocumentException | IOException e) {
}
cellFont = new Font(baseFont);
facetFont = new Font(baseFont);
}
PF 12
默认版本的唯一变化是使用与 Unicode 字符兼容的字体。PF
版本不使用 openpdf
所以你需要 pdfCalligraph
让它与 itext
一起工作。