DavBfr / dart_pdf

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

Receipt on POS Printers Not Taking Full Width #1571

Open MuhammadUmair786 opened 8 months ago

MuhammadUmair786 commented 8 months ago

Describe the bug

  1. I have set up a custom page format using const PdfPageFormat(...), but the content does not extend to the full width.
  2. When "Fit to printable area" is selected, the content aligns center, and there is empty space at the top.

To Reproduce Code snippet to reproduce the behavior:

import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:get/get.dart';

class PrintTest {
  final PdfPageFormat pageFormat;
  final double fontSize;

  PrintTest({required this.pageFormat, required this.fontSize});
}

const double defaultFontSize = 5;

Future<void> printImgTest3() async {
  List<String> toStringList = [
    "PdfPageFormat.a3",
    "PdfPageFormat.a4",
    "PdfPageFormat.a5",
    "PdfPageFormat.a6",
    "PdfPageFormat.legal",
    "PdfPageFormat.letter",
    "PdfPageFormat.roll57",
    "PdfPageFormat.roll80",
    "PdfPageFormat.undefined",
    "28 x 2",
  ];
  Get.generalDialog<PrintTest>(
    pageBuilder: (_, __, ___) {
      TextEditingController widthController = TextEditingController();
      TextEditingController heightController = TextEditingController();

      TextEditingController marginController = TextEditingController(text: '2');
      TextEditingController fontSizeController =
          TextEditingController(text: '$defaultFontSize');
      final GlobalKey<FormState> formKey = GlobalKey<FormState>();

      return Align(
        alignment: Alignment.center,
        child: Container(
          width: 400,
          margin: const EdgeInsets.all(20),
          padding: const EdgeInsets.all(20),
          child: Material(
            color: Colors.white,
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(10),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const SizedBox(height: 15),
                  CustomTextFormFieldWithLabel(
                    labelText: 'Font size',
                    controller: fontSizeController,
                    hintText: 'Default Value: $defaultFontSize',
                    inputFormatterList: acceptDecimalValues(),
                  ),
                  const SizedBox(height: 10),
                  Form(
                    key: formKey,
                    child: Column(
                      children: [
                        CustomTextFormFieldWithLabel(
                          labelText: 'Width',
                          controller: widthController,
                          inputFormatterList: acceptDecimalValues(),
                        ),
                        CustomTextFormFieldWithLabel(
                          labelText: 'Height',
                          controller: heightController,
                          inputFormatterList: acceptDecimalValues(),
                        ),
                        CustomTextFormFieldWithLabel(
                          labelText: 'Margin',
                          controller: marginController,
                          inputFormatterList: acceptDecimalValues(),
                        ),
                        ElevatedButton(
                          onPressed: () {
                            if (formKey.currentState!.validate()) {
                              Get.back(
                                result: PrintTest(
                                  pageFormat: PdfPageFormat(
                                    double.parse(widthController.text) *
                                        PdfPageFormat.mm,
                                    double.parse(heightController.text) *
                                        PdfPageFormat.mm,
                                    marginAll:
                                        double.parse(marginController.text) *
                                            PdfPageFormat.mm,
                                  ),
                                  fontSize: double.tryParse(
                                          fontSizeController.text) ??
                                      defaultFontSize,
                                ),
                              );
                            }
                          },
                          child: const Text('Select Custom Format'),
                        )
                      ],
                    ),
                  ),
                  const SizedBox(height: 10),
                  Wrap(
                    alignment: WrapAlignment.center,
                    children: <PdfPageFormat>[
                      PdfPageFormat.a3,
                      PdfPageFormat.a4,
                      PdfPageFormat.a5,
                      PdfPageFormat.a6,
                      PdfPageFormat.legal,
                      PdfPageFormat.letter,
                      PdfPageFormat.roll57,
                      PdfPageFormat.roll80,
                      PdfPageFormat.undefined,
                      const PdfPageFormat(
                        28 * PdfPageFormat.mm,
                        double.infinity,
                        marginAll: 2 * PdfPageFormat.mm,
                      ),
                    ]
                        .mapIndexed(
                          (e, i) => Padding(
                            padding: const EdgeInsets.all(5),
                            child: InkWell(
                              onTap: () {
                                Get.back(
                                  result: PrintTest(
                                    pageFormat: e,
                                    fontSize: double.tryParse(
                                            fontSizeController.text) ??
                                        defaultFontSize,
                                  ),
                                );
                              },
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Text(toStringList[i]),
                              ),
                            ),
                          ),
                        )
                        .toList(),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    },
    barrierDismissible: true,
    barrierLabel: 'test-print-3',
  ).then((PrintTest? value) async {
    if (value == null) {
      return;
    }

    final doc = pw.Document(); 

    doc.addPage(
      pw.Page(
        pageFormat: value.pageFormat,
        orientation: pw.PageOrientation.portrait,
        build: (pw.Context context) {
          return pw.Column(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            mainAxisSize: pw.MainAxisSize.min,
            children: [
              pw.FittedBox(
                alignment: pw.Alignment.center,
                fit: pw.BoxFit.scaleDown,
                child: pw.Text(
                  'Business Name',
                  style: pw.TextStyle(
                    fontWeight: pw.FontWeight.bold,
                    fontSize: value.fontSize + 1,
                  ),
                ),
              ), 
              pw.SizedBox(height: 1),
              pw.FittedBox(
                alignment: pw.Alignment.center,
                fit: pw.BoxFit.scaleDown,
                child: pw.Text(
                  'Transaction Id: 32',
                  style: pw.TextStyle(
                    fontWeight: pw.FontWeight.bold,
                    fontSize: value.fontSize,
                  ),
                ),
              ),
              pw.FittedBox(
                alignment: pw.Alignment.center,
                fit: pw.BoxFit.scaleDown,
                child: pw.Text(
                  'Dec 2, 2034 1:08 AM',
                  style: pw.TextStyle(
                    fontWeight: pw.FontWeight.bold,
                    fontSize: value.fontSize,
                  ),
                ),
              ),
              pw.Row(
                children: [
                  pw.Expanded(
                    flex: 3,
                    child: pw.SizedBox(
                      height: 10,
                      child: pw.FittedBox(
                        alignment: pw.Alignment.centerLeft,
                        fit: pw.BoxFit.scaleDown,
                        child: pw.Text(
                          'Product',
                          style: pw.TextStyle(
                            fontSize: value.fontSize,
                            fontWeight: pw.FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                  pw.Expanded(
                    flex: 1,
                    child: pw.SizedBox(
                      height: 8,
                      child: pw.FittedBox(
                        alignment: pw.Alignment.center,
                        fit: pw.BoxFit.scaleDown,
                        child: pw.Text(
                          'Qnty.',
                          style: pw.TextStyle(
                            fontWeight: pw.FontWeight.bold,
                            fontSize: value.fontSize,
                          ),
                        ),
                      ),
                    ),
                  ),
                  pw.Expanded(
                    flex: 1,
                    child: pw.SizedBox(
                      height: 8,
                      child: pw.FittedBox(
                        alignment: pw.Alignment.center,
                        fit: pw.BoxFit.scaleDown,
                        child: pw.Text(
                          'Price',
                          style: pw.TextStyle(
                            fontWeight: pw.FontWeight.bold,
                            fontSize: value.fontSize,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              ...[
                1,
                2,
                3,
                4,
                5,
              ].map(
                (e) => pw.Row(
                  children: [
                    pw.Expanded(
                      flex: 3,
                      child: pw.Text(
                        'Product 1 jshkdh jhkash kafhjah fkahka ',
                        style: pw.TextStyle(
                          fontSize: value.fontSize - 1,
                        ),
                      ),
                    ),
                    pw.Expanded(
                      flex: 1,
                      child: pw.SizedBox(
                        height: 7,
                        child: pw.FittedBox(
                          alignment: pw.Alignment.center,
                          fit: pw.BoxFit.scaleDown,
                          child: pw.Text(
                            '2',
                            style: pw.TextStyle(
                              fontSize: value.fontSize - 1,
                            ),
                          ),
                        ),
                      ),
                    ),
                    pw.Expanded(
                      flex: 1,
                      child: pw.SizedBox(
                        height: 7,
                        child: pw.FittedBox(
                          alignment: pw.Alignment.center,
                          fit: pw.BoxFit.scaleDown,
                          child: pw.Text(
                            'Rs.34534',
                            style: pw.TextStyle(
                              fontSize: value.fontSize - 1,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          );
        },
      ),
    );

    await Printing.layoutPdf(
      format: value.pageFormat,
      onLayout: (PdfPageFormat ppf) => doc.save(),
    );
  });
}
[flutter](https://stackoverflow.com/questions/tagged/flutter)

Expected behavior

Screenshots image image

Flutter Doctor

[✓] Flutter (Channel stable, 3.16.0, on macOS 14.2 23C64 darwin-arm64, locale en-PK)
    • Flutter version 3.16.0 on channel stable at /Users/muhammad/fvm/versions/3.16.0
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision db7ef5bf9f (6 weeks ago), 2023-11-15 11:25:44 -0800
    • Engine revision 74d16627b9
    • Dart version 3.2.0
    • DevTools version 2.28.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/muhammad/Library/Android/sdk
    • Platform android-33, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.13.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.85.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 14.2 23C64 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 120.0.6099.129
    ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and
      attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Desktop (please complete the following information):

Smartphone (please complete the following information):

astronaako commented 3 months ago

Any updates on this issue?

MuhammadUmair786 commented 3 months ago

Any updates on this issue?

not yet |: