DavBfr / dart_barcode

Barcode generation library
https://pub.dev/packages/barcode
Apache License 2.0
132 stars 41 forks source link

How can I print the barcode? #41

Closed antoniopetricc closed 2 years ago

antoniopetricc commented 2 years ago

is it possible to print the barcode with my thermal printer connected via usb?

DavBfr commented 2 years ago

If you can print an image, yes. Use this https://pub.dev/packages/barcode_image Otherwise, try with the pdf plugin: https://github.com/DavBfr/dart_pdf/wiki/Document-Output#using-another-plugin

antoniopetricc commented 2 years ago

Thanks for the reply. But if the printer is connected only via usb, what can I do? Is there any way?

DavBfr commented 2 years ago

I don't know

antoniopetricc commented 2 years ago

Ok! I was able to print via my thermal printer. The problem now is the size of the pdf. If the sticker where I print is 50 x 30 mm, how should I set the pdf? Could you give me an example? (With what you gave me from the link it prints me badly!)

DavBfr commented 2 years ago

Use a

SizedBox(
    width: 50 * PdfPageFormat.mm, 
    height: 30 * PdfPageFormat.mm, 
    child: BarcodeWidget(
         barcode: Barcode.ean13,
         data: '12345678'
       ),
);
antoniopetricc commented 2 years ago

If I use 50 and 30 it prints the blank page, to test I also put 500 and 300 and it gives me the giant sticker. What should I put on pageFormat? Now I use "format" but I also saw PdfPageFormat.roll80.

        doc.addPage(
          Page(
            pageFormat: format,
            build: (Context context) {
              return SizedBox(
                width: 500 * PdfPageFormat.mm,
                height: 300 * PdfPageFormat.mm,
                child: BarcodeWidget(
                  barcode: Barcode.ean13(drawEndChar: true),
                  data: '8099807953185',
                ),
              );
            },
          ),
        );
DavBfr commented 2 years ago

I would do:


  Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
    final pdf = pw.Document();

    pdf.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.roll57.copyWith(
          marginTop: 2 * PdfPageFormat.mm,
          marginLeft: 3.5 * PdfPageFormat.mm,
          marginRight: 3.5 * PdfPageFormat.mm,
          marginBottom: 2 * PdfPageFormat.mm,
        ),
        build: (pw.Context context) {
          return pw.Center(
            child: pw.Wrap(
              runSpacing: 2 * PdfPageFormat.mm, // Space between stickers
              children: [
                pw.SizedBox(
                  height: 30 * PdfPageFormat.mm,
                  child: pw.BarcodeWidget(
                    barcode: pw.Barcode.ean13(drawEndChar: true),
                    data: '8099807953185',
                  ),
                ),
                pw.SizedBox(
                  height: 30 * PdfPageFormat.mm,
                  child: pw.BarcodeWidget(
                    barcode: pw.Barcode.ean13(drawEndChar: true),
                    data: '809983425318',
                  ),
                ),
                pw.SizedBox(
                  height: 30 * PdfPageFormat.mm,
                  child: pw.BarcodeWidget(
                    barcode: pw.Barcode.ean13(drawEndChar: true),
                    data: '809980541185',
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );

    return pdf.save();
  }