DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.41k stars 628 forks source link

Issue generating Marathi Language PDF in Flutter #1605

Open MohsinP07 opened 8 months ago

MohsinP07 commented 8 months ago

In my project, according to requirements, I need to generate a PDF in multiple languages. I am able to do so in English perfectly, but I am facing issues when it comes to Marathi or Hindi language. I am getting the generated PDF, but the Marathi and Hindi texts are breaking.

For example: The text I want to print is "पेमेंट रजिस्टर", but inside the PDF print, I am getting the text as pdf_issue

niraj8763 commented 4 weeks ago

same issue...help me

MohsinP07 commented 1 week ago

Hey @niraj8763 , I have solved this issue by using image method, We need to create an image of content first and then print the image which will avoid these issues.

niraj8763 commented 1 week ago

thank for this @MohsinP07 but i have 100 of pages of PDF then that takes time any another solution? my input is this 'जिल्हा परिषद' but I got diffrent output(" जलिहा परषिद") in pdf, pdf.addPage( pw.MultiPage( build: (pw.Context context) {} ) )

MohsinP07 commented 1 week ago

@niraj8763 let me share code snippet with so you can try that

 Future createPDF(List summaryReport) async {
    final pdf = pw.Document();
    final fontData = await rootBundle.load('assets/fonts/Noto-Regular.ttf');
    final font = pw.Font.ttf(fontData);
    final zpImage =
        await _textToImage("जिल्हा परिषद", font, 600, 600, 20, 16);
        pdf.addPage(
      pw.MultiPage(
          build: (context) => [
                pw.Image(pw.MemoryImage(zpImage)),
                pw.SizedBox(height: 4),
                ]),
    );

    return pdf;
  }

  Future savePDF(pw.Document pdf) async {
    final output = await getTemporaryDirectory();
    final file = File("${output.path}/Report.pdf"
        );
    await file.writeAsBytes(await pdf.save());
    return file.path;
  }

  Future _textToImage(String text, pw.Font font, double widthText,
      int widthInt, int height, double fontsize) async {
    final recorder = ui.PictureRecorder();
    final canvas = Canvas(recorder);

    final paragraphStyle = ui.ParagraphStyle(
      fontSize: fontsize,
    );
    final paragraphBuilder = ui.ParagraphBuilder(paragraphStyle)
      ..pushStyle(ui.TextStyle(color: ui.Color(0xFF000000)))
      ..addText(text);
    final paragraph = paragraphBuilder.build()
      ..layout(ui.ParagraphConstraints(width: widthText));

    canvas.drawParagraph(paragraph, Offset(0, 0));

    final picture = recorder.endRecording();
    final img = await picture.toImage(widthInt, height);
    final byteData = await img.toByteData(format: ui.ImageByteFormat.png);

    return byteData!.buffer.asUint8List();
  }  

Also you can call this as

 final pdf = createPDF(summaryReport);
          final filePath = await savePDF(await pdf);
          OpenFile.open(filePath); 

But this has one drawback as well that it takes little extra time in case of large number of pages and please make sure you change the quality of image as per your requirement.

niraj8763 commented 1 week ago

thank @MohsinP07 👍 :) but I have bulk amount marathi/hindi data in multipage page and also table structure is there it takes lot of time, in this scenario copy does not working from pdf.

MohsinP07 commented 5 days ago

@niraj8763 I am also facing the same issue about time and also I have multiple tables in pdf. I tried decreasing the image quality a bit which worked in case of time but its not a long term solution decreasing the quality.