我正在使用 Flutter 开发一个 Web 应用程序。我还在我的项目中使用了syncfusion_flutter_pdf包。我可以在我的项目中成功创建pdf文件。但是,我必须在我的项目中使用土耳其字符。例如,当我尝试打印字母“ş,ğ,ı”时,收到类似“错误:参数无效(字体不支持该字符。):319”的错误。我该如何解决这个问题?
创建 PDF 文件时输入字符“ş,ı,ğ”没有任何错误。
我的代码;
Future<void> generateInvoice() async {
//Create a PDF document.
final PdfDocument document = PdfDocument();
//Add page to the PDF
final PdfPage page = document.pages.add();
//Get page client size
final Size pageSize = page.getClientSize();
//Draw rectangle
page.graphics.drawRectangle(
bounds: Rect.fromLTWH(0, 0, pageSize.width, pageSize.height),
pen: PdfPen(PdfColor(142, 170, 219)));
//Generate PDF grid.
//Draw the header section by creating text element
final PdfLayoutResult result = drawHeader(page, pageSize);
final List<int> bytes = document.saveSync();
//Dispose the document.
document.dispose();
//Save and launch the file.
await saveAndLaunchFile(bytes, 'Invoice.pdf');
}
//Draws the invoice header
PdfLayoutResult drawHeader(PdfPage page, Size pageSize) {
//Draw rectangle
page.graphics.drawRectangle(
brush: PdfSolidBrush(PdfColor(91, 126, 215)),
bounds: Rect.fromLTWH(0, 0, pageSize.width - 115, 90));
//Draw string
page.graphics.drawString(
'iş planı', PdfStandardFont(PdfFontFamily.helvetica, 30),
brush: PdfBrushes.white,
bounds: Rect.fromLTWH(25, 0, pageSize.width - 115, 90),
format: PdfStringFormat(lineAlignment: PdfVerticalAlignment.middle));
}
要在 PDF 页面上显示土耳其语字符,需要使用支持该语言特定字符的合适字体。
使用以下修改后的代码和支持的字体。
//Create a True type font. The fontData is in bytes (unsigned 8-bit integer).
PdfTrueTypeFont font = PdfTrueTypeFont(fontData, 14);
//Draw string
page.graphics.drawString(
'is plani', font,
brush: PdfBrushes.white,
bounds: Rect.fromLTWH(25, 0, pageSize.width - 115, 90),
format: PdfStringFormat(lineAlignment: PdfVerticalAlignment.middle));
请参阅以下 UG 文档了解更多详细信息。 https://help.syncfusion.com/flutter/pdf/working-with-text#draw-text-using-truetype-fonts
请参阅以下知识库文章以获取更多参考,https://support.syncfusion.com/kb/article/10447/how-to-create-a-pdf-file-in-flutter-web-platform https://support.syncfusion.com/kb/article/10391/how-to-read-pdf-file-in-syncfusion-flutter-pdf-library https://support.syncfusion.com/kb/article/10420/how-to-draw-unicode-text-in-a-pdf-file-using-google-fonts-package-in-flutter