import 'package:flutter/services.dart'; import 'package:manager_api_new/api.dart'; import 'dart:convert'; import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart' as pw; import 'dart:html' as html; class PDFHelper { static downloadPDF(List sections) async { var sectionsArticle = sections.where((section) => section.type == SectionType.Article || section.type == SectionType.Quizz); // TODO HERE Support more section types if(sectionsArticle.length > 0) { //final font = await rootBundle.load("fonts/OpenSans-Medium.ttf"); //Uint8List byteData = new File("fonts/OpenSans-Medium.ttf").readAsBytesSync(); //ByteData font = ByteData.view(byteData.buffer); //final ttf = pw.Font.ttf(font); final pdf = pw.Document(); pdf.addPage(pw.MultiPage( pageFormat: PdfPageFormat.a4, build: (pw.Context context) { return [ for(var section in sectionsArticle) pw.Center( child: pw.Column( children: [ pw.Text(section.label!.replaceAll(RegExp("[^A-Za-z0-9---():!éàèëêäâç' ]"), '')), //style: pw.TextStyle(font: ttf, fontSize: 30) pw.Padding(padding: const pw.EdgeInsets.only(top: 20)), pw.BarcodeWidget( data: section.id!, width: 150, height: 150, barcode: pw.Barcode.qrCode(), drawText: false, ), pw.Padding(padding: const pw.EdgeInsets.only(bottom: 30)), ] ) ) ]; })); /*pdf.addPage(pw.Page( pageFormat: PdfPageFormat.a4, build: (pw.Context context) { return pw.Center( child: pw.Column( children: [ pw.Text(section.label, style: pw.TextStyle(font: ttf, fontSize: 30)), pw.Padding(padding: const pw.EdgeInsets.only(top: 10)), pw.BarcodeWidget( data: section.id, width: 150, height: 150, barcode: pw.Barcode.qrCode(), drawText: false, ), ], ) ); }));*/ var test = await pdf.save(); final content = base64Encode(test); final fileName = sectionsArticle.length > 1 ? 'QRCodes' : sectionsArticle.first.label; final anchor = html.AnchorElement( href: "data:application/octet-stream;charset=utf-16le;base64,$content") ..setAttribute("download", '$fileName.pdf') ..click(); } } }