Flutter Printing Library - Pos Receipt Not Taking Full Width

I am using the printing: ^5.11.1 in Flutter to generate a POS receipt, but I am encountering an issue where the content is not taking full width on the printed page. When I select "Fit to printable area", the content aligns center, and there is wasted space at the top of the page.

Screenshot when Fit to printable area

Screenshot with default setting

Issue Description:

  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.

Question:

  1. How can I ensure that the content takes full width on the printed page?

  2. Is there a way to adjust the layout so that the content is aligned at the top of the page when "Fit to printable area" is selected?

Here is a simplified version of my code:

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(),
    );
  });
}

Upvotes: 1

Views: 154

Answers (0)

Related Questions