How to generate QR Code on PDF file in flutter

511

I had a problem showing that line "has no Unicode support..." to fix that i created a folder fonts on my project and added some true type fonts to it, then i created a pdf theme for the document like below:

var myTheme = ThemeData.withFont(
      base: Font.ttf(await rootBundle.load("fonts/OpenSans-Regular.ttf")),
      bold: Font.ttf(await rootBundle.load("fonts/OpenSans-Bold.ttf")),
      italic: Font.ttf(await rootBundle.load("fonts/OpenSans-Italic.ttf")),
      boldItalic:
          Font.ttf(await rootBundle.load("fonts/OpenSans-BoldItalic.ttf")),
    );

then i used the theme in the page of the document and the error was gone, like below:

Page(theme: myTheme,...)

I'm not sure if is the same case, but i think you should try.

Share:
511
SkillzUPP Technologies
Author by

SkillzUPP Technologies

Updated on December 29, 2022

Comments

  • SkillzUPP Technologies
    SkillzUPP Technologies over 1 year

    I have used this solution How Can I generate QR code and Show it on a PDF page in Flutter but still QR Code isn't being printed.

    My code:

      pdf.BarcodeWidget(
                  color: PdfColor.fromHex("#000000"),
                  barcode:pdf.Barcode.qrCode(), data: "My name is Bilal"
              ),
    

    This is my function:

    Future<Uint8List> func_generate_sale_pdf(PdfPageFormat format) {
        final doc = pdf.Document();
    
        doc.addPage(pdf.Page(build: (pdf.Context context) {
          return pdf.Center(
            child: pdf.Column(children: <pdf.Widget>[
    
              pdf.Text(config.app_name.toUpperCase(),
                  style:
                      pdf.TextStyle(fontSize: 32, fontWeight: pdf.FontWeight.bold)),
              pdf.SizedBox(height: 25),
              pdf.Text(
                  " ${languages.skeleton_language_objects[config.app_language]['for_enquiries_call']} : ${config.app_contacts}",
                  style: pdf.TextStyle(fontSize: 22)),
              pdf.SizedBox(height: 5),
              pdf.Text("${config.app_po_box}", style: pdf.TextStyle(fontSize: 22)),
              pdf.SizedBox(height: 5),
              pdf.Text(
                  "${languages.skeleton_language_objects[config.app_language]['receipt']} # ${receipt_data['receipt_no']}",
                  style: pdf.TextStyle(fontSize: 20)),
              pdf.SizedBox(height: 5),
              pdf.Text("${receipt_data['date_time']}",
                  style: pdf.TextStyle(fontSize: 20)),
              pdf.SizedBox(height: 30),
              pdf.Container(
                alignment: pdf.Alignment.topLeft,
                child: pdf.Text("${receipt_data['items_list'].toString()}",
                    style: pdf.TextStyle(
                        fontSize: 22, fontWeight: pdf.FontWeight.normal),
                    textAlign: pdf.TextAlign.left),
              ),
              pdf.SizedBox(height: 30),
              pdf.Container(
                  child: pdf.Row(children: <pdf.Widget>[
                pdf.Expanded(
                  flex: 1,
                  child: pdf.Text(
                      languages.skeleton_language_objects[config.app_language]
                          ['total_amount'],
                      style: pdf.TextStyle(
                          fontSize: 20, fontWeight: pdf.FontWeight.bold),
                      textAlign: pdf.TextAlign.left),
                ),
                pdf.Expanded(
                  flex: 1,
                  child: pdf.Text("${receipt_data['total_amount']}",
                      style: pdf.TextStyle(fontSize: 20),
                      textAlign: pdf.TextAlign.right),
                ),
              ])),
              pdf.SizedBox(height: 10),
              pdf.Container(
                  child: pdf.Row(children: <pdf.Widget>[
                pdf.Expanded(
                  flex: 1,
                  child: pdf.Text(
                      languages.skeleton_language_objects[config.app_language]
                          ['total_items'],
                      style: pdf.TextStyle(
                          fontSize: 20, fontWeight: pdf.FontWeight.bold),
                      textAlign: pdf.TextAlign.left),
                ),
                pdf.Expanded(
                  flex: 1,
                  child: pdf.Text("${receipt_data['total_items']}",
                      style: pdf.TextStyle(fontSize: 20),
                      textAlign: pdf.TextAlign.right),
                ),
              ])),
              pdf.SizedBox(height: 10),
              pdf.Container(
                  height: 35,
                  child: pdf.Row(children: <pdf.Widget>[
                    pdf.Expanded(
                      flex: 1,
                      child: pdf.Text(
                          languages.skeleton_language_objects[config.app_language]
                              ['total_tax'],
                          style: pdf.TextStyle(
                              fontSize: 20, fontWeight: pdf.FontWeight.bold),
                          textAlign: pdf.TextAlign.left),
                    ),
                    pdf.Expanded(
                      flex: 1,
                      child: pdf.Text("${receipt_data['total_tax_amount']}",
                          style: pdf.TextStyle(fontSize: 20),
                          textAlign: pdf.TextAlign.right),
                    ),
                  ])),
              pdf.SizedBox(height: 35),
              pdf.Text(
                  languages.skeleton_language_objects[config.app_language]
                      ['thant_you_and_come_again'],
                  style: pdf.TextStyle(fontSize: 24)),
    
    
             //From here on, the compiler doesn't print anything.
              pdf.BarcodeWidget(
                  color: PdfColor.fromHex("#000000"),
                  barcode:pdf.Barcode.qrCode(), data: "My name is Bilal"
              ),
              pdf.Text("SkillzUPP Technologies: +923058431046",
                  style: pdf.TextStyle(fontSize: 24),
                  textAlign: pdf.TextAlign.center),
              pdf.SizedBox(height: 10),
    
            ]),
          ); // Center
        }));
    
        return doc.save();
      }
    

    Everything prints fine except QR Code and at that specific line this error is shown at terminal:

    Helvetica-Bold has no Unicode support see https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management
    
  • Bilal Saeed- Flutter Developer
    Bilal Saeed- Flutter Developer almost 3 years
    Need elaboration. I have edited my question.
  • Jorge Vieira
    Jorge Vieira almost 3 years
    I edited my answer, i think could help ,i hope.
  • Bilal Saeed- Flutter Developer
    Bilal Saeed- Flutter Developer almost 3 years
    I am getting this error : The method 'withFont' isn't defined for the type 'Theme'.. And compiler isn't suggesting any library to import. Moreover Font is also not recognized Undefined name 'Font'. May be that's because Flutter 2.0.4 has some different way to perform it...
  • Jorge Vieira
    Jorge Vieira almost 3 years
    They are from this package, same pdf package, just different file, import 'package:pdf/widgets.dart';
  • Bilal Saeed- Flutter Developer
    Bilal Saeed- Flutter Developer almost 3 years
    At last I made it. Thanks for your help. At a point after all was set, I wasn't getting the QR code displayed in pdf. Then I gave height: 70 ,width :70 in Constructor of BarcodeWidget. And it worked.