DavBfr / dart_pdf

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

Mid-word line break when generating a table using TableHelper.fromTextArray #1618

Open insinfo opened 6 months ago

insinfo commented 6 months ago

Describe the bug Mid-word line break when generating a table using TableHelper.fromTextArray

To Reproduce

 Future<void> exportPdf([bool isPrint = false, bool isDownload = true]) async {
    final loading = SimpleLoading();
    loading.show();
    try {
      final logoUrlSvg = '/assets/images/brasao_editado_1.svg';
      final svgRaw = await FrontUtils.getNetworkTextFile(logoUrlSvg);
      final svgImageLogo = pdf.SvgImage(svg: svgRaw);
      final docPdf = pdf.Document();
      final now = DateTime.now();

      final headerTextStyle =
          pdf.TextStyle(fontSize: 10, color: pdf.PdfColor.fromInt(0xff2c3e50));

      //visibility
      final tableItems = <List<String>>[];
      tableItems.add(settings.colsDefinitions
          .where((c) => c.visibility)
          .map((e) => e.title)
          .toList());

      tableItems.addAll(rows.map((row) => row.columns
          .where((c) => c.visibility)
          .map((col) => stripHtml(col.value))
          .toList()));

      docPdf.addPage(
        pdf.MultiPage(
          maxPages: 6000,
          orientation: pdf.PageOrientation.landscape,
          pageFormat: pdf.PdfPageFormat.a3.copyWith(
            marginTop: 1.0 * pdf.PdfPageFormat.cm,
            marginLeft: 1.0 * pdf.PdfPageFormat.cm,
            marginRight: 1.0 * pdf.PdfPageFormat.cm,
            marginBottom: 1.0 * pdf.PdfPageFormat.cm,
          ),
          crossAxisAlignment: pdf.CrossAxisAlignment.start,
          header: (pdf.Context context) {
            return pdf.Padding(
                padding: pdf.EdgeInsets.only(bottom: 10),
                child: pdf.Row(
                  mainAxisAlignment: pdf.MainAxisAlignment.spaceBetween,
                  children: [
                    pdf.Column(
                      children: [
                        pdf.Text('Prefeitura Municipal de Rio das Ostras',
                            style: headerTextStyle),
                        pdf.Text(
                            'Rua Campo do Albacora, nº 75 - Loteamento Atlântica',
                            style: headerTextStyle),
                        pdf.Text('CEP: 28895-664 | Tel.: (22) 2771-1515',
                            style: headerTextStyle),
                        pdf.Text(
                            '''Emissão: ${DateFormat("dd/MM/yyyy 'às' HH:mm").format(now)}   |   Página: ${context.pageNumber} de ${context.pagesCount}''',
                            style: headerTextStyle),
                      ],
                      crossAxisAlignment: pdf.CrossAxisAlignment.start,
                    ),
                    pdf.SizedBox(
                      height: 70,
                      width: 140,
                      child: svgImageLogo, //svgImage, //Image(imageLogo),
                    )
                  ],
                ));
          },
          build: (pdf.Context context) => <pdf.Widget>[

            pdf.TableHelper.fromTextArray(context: context, data: tableItems),
          ],
          footer: (pdf.Context context) {
            return pdf.Container(
              width: double.infinity,
              decoration: pdf.BoxDecoration(
                  border: pdf.Border(
                      top: pdf.BorderSide(color: pdf.PdfColors.grey))),
              child: pdf.Padding(
                padding: pdf.EdgeInsets.only(top: 5),
                child: pdf.Text('Sistema da PMRO - ${DateTime.now().year}',
                    style:
                        pdf.TextStyle(fontSize: 9, color: pdf.PdfColors.grey)),
              ),
            );
          },
        ),
      );

      //save PDF
      final bytes = await docPdf.save();
      if (isDownload) {
        FrontUtils.downloadFile(bytes, 'Relatório.pdf', 'application/pdf');
      }
      if (isPrint) {
        FrontUtils.printFileBytes(bytes, 'application/pdf');
      }
    } catch (e) {
      print('Errro on generate PDF $e');
    } finally {
      loading.hide();
    }
  }

Expected behavior the output should not break in the middle of words

Screenshots

html page

image

pdf

image

Desktop (please complete the following information):

Relatório (5).pdf data.xlsx

Dart SDK version: 3.2.1 (stable) (Wed Nov 22 08:59:13 2023 +0000) on "windows_x64" pdf: 3.10.8

MathanM commented 4 months ago

I am facing the same issue, Is there a way to prevent breaking of letters in a word ? Breaking words is fine but it shouldn't break single word

MathanM commented 1 month ago

@DavBfr I am thinking about finding a solution to this issue. I have found a approach of adding minWidth to pdfRect and it will basically store minimum width that widget can take, specifically to address scenarios like this where we want to prevent text widget to go beyond where we need to break words. I will try to come up the draft PR, but definitely need you suggestion as well.