David PHAM-VAN

Add TableOfContent Widget

<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 150 230"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="gradient-1">
<stop offset="0" style="stop-color: rgb(255, 255, 255);"/>
<stop offset="1" style="stop-color: rgb(161, 161, 161);"/>
</linearGradient>
<linearGradient id="gradient-1-0" gradientUnits="userSpaceOnUse" x1="119.315" y1="36.313" x2="119.315" y2="216.359" xlink:href="#gradient-1"/>
</defs>
<rect x="-67.203" y="-48.72" width="276.134" height="354.501" style="fill: rgb(93, 32, 205);"/>
<ellipse style="fill: rgb(141, 81, 252);" cx="119.315" cy="126.336" rx="139.238" ry="98.673" transform="matrix(0.93642, 0.35088, -0.35088, 0.93642, -15.610807, -59.255697)"/>
<ellipse style="fill: url(#gradient-1-0);" cx="119.315" cy="126.336" rx="130.398" ry="90.023" transform="matrix(0.952861, 0.303408, -0.303408, 0.952861, -26.924128, -56.954295)"/>
</svg>
... ...
... ... @@ -28,6 +28,72 @@ Future<Uint8List> generateDocument(
final font1 = await rootBundle.load('assets/open-sans.ttf');
final font2 = await rootBundle.load('assets/open-sans-bold.ttf');
final shape = await rootBundle.loadString('assets/document.svg');
doc.addPage(
pw.Page(
pageTheme: pw.PageTheme(
pageFormat: format.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
orientation: pw.PageOrientation.portrait,
buildBackground: (context) => pw.FullPage(
ignoreMargins: true,
child: pw.SvgImage(svg: shape, fit: pw.BoxFit.fill),
),
),
build: (context) {
return pw.Column(
children: [
pw.Spacer(),
pw.RichText(
text: pw.TextSpan(children: [
pw.TextSpan(
text: DateTime.now().year.toString() + '\n',
style: pw.TextStyle(
fontWeight: pw.FontWeight.bold,
color: PdfColors.grey600,
fontSize: 40,
),
),
pw.TextSpan(
text: 'Portable Document Format',
style: pw.TextStyle(
fontWeight: pw.FontWeight.bold,
fontSize: 40,
),
),
])),
pw.Spacer(),
pw.Container(
alignment: pw.Alignment.topRight,
height: 150,
child: pw.PdfLogo(),
),
pw.Spacer(flex: 2),
],
);
},
),
);
doc.addPage(
pw.Page(
pageFormat: format.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
orientation: pw.PageOrientation.portrait,
build: (context) {
return pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Center(
child: pw.Text('Table of content',
style: pw.Theme.of(context).header0),
),
pw.SizedBox(height: 20),
pw.TableOfContent(),
],
);
},
),
);
doc.addPage(pw.MultiPage(
theme: pw.ThemeData.withFont(
... ...
# Changelog
## 3.3.1
## 3.4.0
- Fix Text.softWrap behavior
- Add TableOfContent Widget
## 3.3.0
... ...
... ... @@ -58,7 +58,9 @@ class PdfOutline extends PdfObjectDict {
this.color,
this.destMode = PdfOutlineMode.fitPage,
this.style = PdfOutlineStyle.normal,
int? page,
}) : assert(anchor == null || (dest == null && rect == null)),
_page = page,
super(pdfDocument);
/// This holds any outlines below us
... ... @@ -73,6 +75,12 @@ class PdfOutline extends PdfObjectDict {
/// The destination page
PdfPage? dest;
/// Page number
int? get page =>
_page ??
(dest != null ? pdfDocument.pdfPageList.pages.indexOf(dest!) + 1 : null);
final int? _page;
/// The region on the destination page
final PdfRect? rect;
... ...
... ... @@ -255,6 +255,7 @@ class Outline extends Anchor {
anchor: name,
color: color,
style: style,
page: context.pageNumber,
);
var parent = context.document.outline;
... ...
... ... @@ -25,6 +25,7 @@ import 'container.dart';
import 'decoration.dart';
import 'flex.dart';
import 'geometry.dart';
import 'multi_page.dart';
import 'text.dart';
import 'text_style.dart';
import 'theme.dart';
... ... @@ -132,6 +133,49 @@ class Header extends StatelessWidget {
}
}
class TableOfContent extends StatelessWidget {
Iterable<Widget> _buildToc(PdfOutline o, int l) sync* {
for (final c in o.outlines) {
if (c.title != null) {
yield Padding(
padding: const EdgeInsets.only(bottom: 2),
child: Link(
destination: c.anchor!,
child: Row(
children: [
SizedBox(width: 10.0 * l),
Text('${c.title}'),
SizedBox(width: 8),
Expanded(
child: Divider(
borderStyle: BorderStyle.dotted,
thickness: 0.2,
)),
SizedBox(width: 8),
Text('${c.page}'),
],
),
),
);
yield* _buildToc(c, l + 1);
}
}
}
@override
Widget build(Context context) {
assert(context.page is! MultiPage,
'$runtimeType will not work with MultiPage');
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
..._buildToc(context.document.outline, 0),
],
);
}
}
class Paragraph extends StatelessWidget {
Paragraph({
this.text,
... ...
... ... @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 3.3.1
version: 3.4.0
environment:
sdk: ">=2.12.0-0 <3.0.0"
... ...