I want to generate a Pdf from a Html file
I used ITextRenderer.
The PDf is generated without problems in english and french
I have a problem to generate an Arabic Pdf the characters in Arabic are not shown.
I tried to add a font that supports the Arabic, but it seemes that the font is not applied (the font already exists in my PC)
ITextRenderer renderer = new ITextRenderer();
if (language.equals("fr")) {
template = "LocationBillTemplateFr";
} else if (language.equals("en")) {
template = "LocationBillTemplateEn";
} else {
template = "LocationBillTemplateAr";
renderer.getFontResolver().addFont("C:\\WINDOWS\\Fonts\\simpbdo.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
TemplateEngine templateEngine = PdfGenerationUtils.prepareTemplateHtmlWithPdf();
String renderedHtmlContent = templateEngine.process(template, context);
String xHtml = PdfGenerationFromHtmlUtils.convertToXhtml(renderedHtmlContent);
renderer.setDocumentFromString(xHtml, PdfGenerationFromHtmlUtils.baseUrl());
renderer.layout();
String absolutePath = environment.getProperty("app.payment.receipt.location.folder");
OutputStream outputStream = new FileOutputStream(
absolutePath + File.separator + "Payment" + subscriptionId + ".pdf");
renderer.createPDF(outputStream);
outputStream.close();
Related
export excel with Header(return XSSFOddHeader),transform to PDF,it show me the mess code when i input Chinese.
I tried to transform the stirng encode and set the response header and contenttype,but it not working.
Later I tested, even if utf-8 is not added to the new String, the corresponding output hex is the same, so the default is to output utf-8 format, but I don't know the encoding format of poi output, and how to solve this problem
I use poi-3.17.jar, here is my code and image.
response.setContentTypeIfNotSet("text/html; charset=UTF-8");
response.setHeader("text/html; charset=UTF-8", "text/html; charset=UTF-8");
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);// 建工作薄
workbook.createSheet("哈喽");// 创建一个表
SXSSFSheet toSheet1 = workbook.getSheetAt(0);
// 设置页眉、页脚
Header header = toSheet1.getHeader();
header.setLeft(DateUtil.formatDate(mmAnbuymaterialInfo.getCraetetime()));
String newStr ="你好世界/PO NO: "+ mmAnbuymaterialInfo.getPurchaseno();
byte[] bs = newStr.getBytes();
String UTF_8String = new String(bs, "UTF-8");
header.setCenter(UTF_8String);
// convert to PDF code
String path = Play.applicationPath + getTmpFilePath(sheetName);
// 根据生成的
File file = new File(Play.applicationPath + UPLOAD_TMPDIR + File.separator + subdir);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
// 根据generateFilePath方法生成的结算清单xlsx转换成pdf形式,并将路径返回
// 输出pdf文件的路径
String pdfPath = path.substring(0, path.lastIndexOf(".")) + ".pdf";
File pdfFile = new File(pdfPath);// 输出路径
Workbook wb = new Workbook(path);// 原始excel路径
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
mess code image
not convert to PDF image
I'm using the following set-up:
Java 11.0.1
pdfbox 2.0.15
Objective: Rendering a pdf that contains Chinese characters
Problem: java.lang.IllegalArgumentException: U+674E is not available in this font's encoding: WinAnsiEncoding
I already tried:
Using different fonts for Chinese character support. The latest one is NotoSansCJKtc-Regular.ttf
Set font to unicode as described here: Java: Write national characters to PDF using PDFBox, however the used loadTTF method is deprecated.
Using Arial-Unicode-MS_4302.ttf
My code looks like this (shortened a bit):
try (InputStream pdfIn = inputStream; PDDocument pdfDocument =
PDDocument.load(pdfIn)) {
PDFont formFont;
//Check if Chinese characters are present
if (!Util.containsHanScript(queryString)) {
formFont = PDType0Font.load(pdfDocument,
PdfReportGenerator.class.getResourceAsStream("LiberationSans-Regular.ttf"),
false);
} else {
formFont = PDType0Font.load(pdfDocument,
PdfReportGenerator.class.getResourceAsStream("NotoSansCJKtc-Regular.ttf"),
false);
}
List<PDField> fields = acroForm.getFields();
//Load fields into Map
Map<String, PDField> pdfFields = new HashMap<>();
for (PDField field : fields) {
String key = field.getPartialName();
pdfFields.put(key, field);
}
PDField currentField = pdfFields.get("someFieldID");
PDVariableText pdfield = (PDVariableText) currentField;
PDResources res = acroForm.getDefaultResources();
String fontName = res.add(formFont).getName();
String defaultAppearanceString = "/" + fontName + " 10 Tf 0 g";
pdfield.setDefaultAppearance(defaultAppearanceString);
pdfield.setValue("李柱");
acroForm.flatten(fields, true);
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
pdfDocument.save(pdfOut);
}
Expected result: Chinese characters on pdf.
Actual result: java.lang.IllegalArgumentException: U+674E is not available in this font's encoding: WinAnsiEncoding
So my question is about how to best support rendering of Chinese characters with pdfbox. Any help is appreciated.
The following code works for me, it uses the file of PDFBOX-4629:
PDDocument doc = PDDocument.load(new URL("https://issues.apache.org/jira/secure/attachment/12977270/Report_Template_DE.pdf").openStream());
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
PDVariableText field = (PDVariableText) acroForm.getField("search_query");
List<PDField> fields = acroForm.getFields();
PDFont font = PDType0Font.load(doc, new FileInputStream("c:/windows/fonts/arialuni.ttf"), false);
PDResources res = acroForm.getDefaultResources();
String fontName = res.add(font).getName();
String defaultAppearanceString = "/" + fontName + " 10 Tf 0 g";
field.setDefaultAppearance(defaultAppearanceString);
field.setValue("李柱");
acroForm.flatten(fields, true);
doc.save("saved.pdf");
doc.close();
Trying to write heart symbol in pdf through java code .
This is my input to pdf : ❤️❤️❤️
But pdf generated is empty without anything written.
Using itext to write to pdf.
The Font used is tradegothic_lt_boldcondtwenty.ttf
OutputStream file = new FileOutputStream(fileName);
Document document = new Document(PageSize.A6);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfLayer nested = new PdfLayer("Layer 1", writer);
PdfContentByte cb = writer.getDirectContent();
cb.beginLayer(nested);
ColumnText ct = new ColumnText(cb);
Font font = getFont();
Phrase para1 = new Phrase("❤️❤️❤️",font);
ct.setSimpleColumn(para1,38,0,260,138,15, Element.ALIGN_LEFT);
ct.go();
cb.endLayer();
document.close();
file.close();
private Font getFont() {
final String methodName = "generatePDF";
LOGGER.entering(CLASSNAME, methodName);
Font font = null;
try {
String filename = tradegothic_lt_boldcondtwenty.ttf;
FontFactory.register(filename, filename);
font = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED,11.8f);
} catch(Exception exception) {
LOGGER.logp(Level.SEVERE, CLASSNAME, methodName, "Exception Occurred while fetching the Trade Gothic font." + exception);
font = FontFactory.getFont(FontFactory.HELVETICA_BOLD,11.8f);
}
return font;
}
Phrase para1 has the heart correctly. But not able to see in pdf
I'm trying to display an arabic string in a pdf file generated using PDFBox, Actually i can display an arabic string from RTL using ICU4J and a specific font but the problem is this :
even if the string appear correctly from RTL the characters still separated and even if i try some fonts the problem is not solved yet.
Here is a snippet of code used for test:
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
String dir = "resources/fonts/";
//I've used several fonts
PDType0Font farialUni = PDType0Font.load(document, new File(dir + "ARIALUNI.ttf"));
PDFont fArabType = PDType0Font.load(document, new File(dir + "arabtype.ttf"));
PDFont fMuka = PDType0Font.load(document, new File(dir + "Mukadimah.ttf"));
PDFont fFreeSans = PDType0Font.load(document, new File(dir + "FreeSans.ttf"));
PDFont fNoto = PDType0Font.load(document, new File(dir + "NotoNaskhArabic-Regular.ttf"));
PDPageContentStream stream = new PDPageContentStream(document, page);
stream.beginText();
stream.setFont(fNoto, 12);
stream.setLeading(12 * 1.2);
stream.newLineAtOffset(100, 800);
//Switch text order from to RTL
BiDiClass bidiClass = new BiDiClass();
String arabicText = "\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064A\u0643\u0645 ";
//Use icu to inverse the order
String out = bidiClass.makeLineLogicalOrder(arabicText, true);
System.out.println(out);
stream.showText(out);
stream.newLine();
// ligature
stream.showText(out);
stream.endText();
stream.close();
document.save("example.pdf");
document.close();
The resulting string that this code give me is like this :
ال س ل ام ع ل ي ك م
Note: i add the space character in the resulting string only for clarity.
I want to convert my XHTML text to a PDF. I converted it to FileOutputStream but I ca'nt find a way to pass it as an input to the ITextRenderer. Is that possible, and how?
the code :
String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
// renderer.loadDocument(finalXhtmlStream); i can pass a file here can i pass an input or output stream ?
renderer.layout();
renderer.createPDF(os) ;
os.close();
System.out.println("done.");
note: I can pass a file to the ITextRenderer as following:
String File_To_Convert = "report.xhtml";
String url = new File(File_To_Convert).toURI().toURL().toString();
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
System.out.println("done.");
please let me know if I have to provide more details.
I am using following code to export HTML data to PDF with following code:
renderer.setDocumentFromString(htmls.toString());
renderer.layout();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".pdf\"");
renderer.createPDF(outputStream);
renderer.createPDF(fos);
Now here I am using inline CSS to generate PDF using style but is there any option that I can use setDocumentFromString() function by loading external CSS.
I am assuming you are using Flying Saucer. ITextRenderer has a method that does something similar:
public void setDocumentFromString(String content) {
InputSource is = new InputSource(new BufferedReader(new StringReader(content)));
Document dom = XMLResource.load(is).getDocument();
setDocument(dom, null);
}
Adapting your code, what you'd want would look something like this:
String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
Document document = XMLResource.load(finalXhtmlStream).getDocument();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(document, null);
renderer.layout();
renderer.createPDF(os) ;
os.close();
of course you could also do this and skip the inputstream all together:
renderer.setDocumentFromString(finalXhtml);