manager-app/lib/Helpers/PDFHelper.dart

73 lines
2.7 KiB
Dart

import 'package:flutter/services.dart';
import 'package:managerapi/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<SectionDTO> sections) async {
var sectionsArticle = sections.where((section) => section.type == SectionType.article);
if(sectionsArticle.length > 0) {
final font = await rootBundle.load("fonts/OpenSans-Medium.ttf");
//final ttf = pw.Font.ttf(font);
final pdf = pw.Document();
pdf.addPage(pw.MultiPage(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return <pw.Widget> [
for(var section in sectionsArticle)
pw.Center(
child:
pw.Column(
children: [
pw.Text(section.label, style: pw.TextStyle(fontSize: 30)), //font: ttf,
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();
}
}
}