Skip to content

Instantly share code, notes, and snippets.

@DavBfr
Last active March 25, 2022 13:27
Show Gist options
  • Save DavBfr/a70e763582e95f70331d14ad03ddfe7d to your computer and use it in GitHub Desktop.
Save DavBfr/a70e763582e95f70331d14ad03ddfe7d to your computer and use it in GitHub Desktop.
PDF with pictures
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
void main() => runApp(const MyApp('Some pictures'));
class MyApp extends StatelessWidget {
const MyApp(this.title);
final String title;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text(title)),
body: PdfPreview(
build: (format) => _generatePdf(format, title),
),
),
);
}
Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
final pdf = pw.Document();
final font = await PdfGoogleFonts.playRegular();
const urls = [
'https://i.imgur.com/9OPSnFH.jpeg',
'https://i.imgur.com/54Njk5Y.jpeg',
'https://i.imgur.com/YoDEqLF.jpeg',
'https://i.imgur.com/KALDohV.jpeg',
'https://i.imgur.com/JRAmHIq.jpeg',
'https://i.imgur.com/bvVlG2I.jpeg',
'https://i.imgur.com/UPAPKVX.jpeg',
'https://i.imgur.com/O8ThKb2.jpeg',
'https://i.imgur.com/LEbNkdn.jpeg',
];
final images = <pw.ImageProvider>[];
for (final photo in urls) {
images.add(await networkImage(photo));
}
pdf.addPage(
pw.MultiPage(
pageFormat: format,
build: (pw.Context context) => <pw.Widget>[
pw.Center(
child: pw.Text(
title,
style: pw.TextStyle(
fontSize: 28,
font: font,
),
),
),
pw.SizedBox(height: 20),
pw.Center(
child: pw.Wrap(
runSpacing: 10,
spacing: 10,
children: <pw.Widget>[
for (final image in images)
pw.ClipRRect(
horizontalRadius: 10,
verticalRadius: 10,
child: pw.Image(
image,
height: 200,
width: 200,
fit: pw.BoxFit.cover,
),
),
],
),
),
],
),
);
return pdf.save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment