PDF creating awful slow
Describe the bug
Application, using PRINTING, needs minutes to show the pdf. An older version was fast with the same table and same values.
To Reproduce Code snippet to reproduce the behavior:
**pdf_stroke_page.dart**
import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';
import 'package:valuation_pre_shot/constants.dart';
import 'package:valuation_pre_shot/methods/pdf_methods.dart';
class PdfStrokePage extends StatelessWidget {
final List<String> allStrokes;
// final List<List<String>> strokeTable;
final String routineElement;
final String aTitle;
final String aTableHeader;
final String aSubHeader;
final String aRoutineText;
final String formattedDate;
const PdfStrokePage(
{super.key,
required this.allStrokes,
// required this.strokeTable,
required this.routineElement,
required this.aTitle,
required this.aTableHeader,
required this.aSubHeader,
required this.aRoutineText,
required this.formattedDate});
@override
Widget build(BuildContext context) {
final strokeTable = createStrokeTable(allStrokes, numberOfTees,
numberOfStrokesPerTee, initialValue, aTableHeader);
//final Future<Uint8List> aPdfTable = generatePdf(PdfPageFormat.a4, routineElement,
// aSubHeader, aRoutineText, formattedDate, strokeTable);
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text(aTitle),
),
body: PdfPreview(
// build: (format) => aPdfTable,
build: (format) => generatePdf(PdfPageFormat.a4, routineElement, aSubHeader, aRoutineText, formattedDate, strokeTable),
),
),
);
}
}
**pdf_methods.dart**
import 'dart:typed_data';
//import 'package:flutter/services.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';
Future<Uint8List> generatePdf(
PdfPageFormat format,
String routineElement,
String aSubHeader,
String rountineText,
String formattedDate,
List<List<String>> strokeTable) async {
// Text styles
final ttf = await fontFromAssetBundle('assets/fonts/NotoSans-Medium.ttf');
pw.TextStyle headerStyle = pw.TextStyle(fontSize: 16, font: ttf);
pw.TextStyle cellStyle = pw.TextStyle(fontSize: 14, font: ttf);
final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
pdf.addPage(
pw.Page(
pageFormat: format,
build: (context) {
return pw.Column(
children: [
pw.Container(
alignment: pw.Alignment.centerLeft,
child: pw.Text(aSubHeader + formattedDate, style: headerStyle),
),
pw.Container(
alignment: pw.Alignment.centerLeft,
child: pw.Text(rountineText + routineElement, style: headerStyle),
),
pw.SizedBox(height: 40),
pw.TableHelper.fromTextArray(
data: strokeTable,
cellAlignment: pw.Alignment.topRight,
cellStyle: cellStyle,
headerStyle: headerStyle,
),
],
);
},
),
);
return pdf.save();
}
List<List<String>> createStrokeTable(List<String> allStrokes, int numberOfTees,
int numberOfStrokesPerTee, String initialValue, String aTableHeader) {
int teeIndex = 0;
int strokeIndex = 0;
List<List<String>> strokeTable = List.generate(
numberOfTees + 1,
(index) => List.generate(
numberOfStrokesPerTee + 1, (int index) => initialValue,
growable: false),
growable: false,
);
strokeTable[0][0] = aTableHeader;
for (int i = 1; i <= numberOfStrokesPerTee; i++) {
strokeTable[0][i] = i.toString();
}
for (int j = 1; j <= numberOfTees; j++) {
strokeTable[j][0] = j.toString();
}
for (int i = 0; i < allStrokes.length; i++) {
teeIndex = i ~/ numberOfStrokesPerTee;
strokeIndex = i % numberOfStrokesPerTee;
strokeTable[teeIndex + 1][strokeIndex + 1] = allStrokes[i];
}
return strokeTable;
}
Expected behavior
As in a former version pdf should generate fast and show up immediately Screenshots
Flutter Doctor
PS C:\Users\chrbu\Documents\Development\app_two\valuation_pre_shot> flutter doctor -v [√] Flutter (Channel stable, 3.24.4, on Microsoft Windows [Version 10.0.26100.2033], locale de-DE) • Flutter version 3.24.4 on channel stable at C:\Users\chrbu\Documents\Development\flutter\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 603104015d (3 weeks ago), 2024-10-24 08:01:25 -0700 • Engine revision db49896cf2 • Dart version 3.5.4 • DevTools version 2.37.3
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at C:\Users\chrbu\AppData\Local\Android\sdk • Platform android-34, build-tools 34.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314) • All Android licenses accepted.
[√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[X] Visual Studio - develop Windows apps X Visual Studio not installed; this is necessary to develop Windows apps. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2024.1) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = C:\Program Files\Android\Android Studio • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)
[√] VS Code (version 1.95.2) • VS Code at C:\Users\chrbu\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.100.0
[√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.26100.2033] • Chrome (web) • chrome • web-javascript • Google Chrome 130.0.6723.117 • Edge (web) • edge • web-javascript • Microsoft Edge 130.0.2849.80
[√] Network resources • All expected network resources are available.
! Doctor found issues in 1 category.
Desktop (please complete the following information):
- [ ] iOS
- [x ] Android
- [ ] Browser
- [x ] Windows
- [ ] Linux
Smartphone (please complete the following information):
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
Additional context
I add to this version internationalisation and persistence: import 'package:provider/provider.dart'; import 'package:valuation_pre_shot/language_persistent.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
May be there is a conflict or a lack of memory?
Please try with this PR #1776 see if it improves performance.
Thank you, but I couldn't apply mentioned PR. I put the call of createStrokeTable into generatePdf. This simplified the generation of the document. But it still lasts minutes until show it. If I disturb the application by minimize and maximize the screen it seems like that the pdf will be shown faster.
Please, close this issue. Printing and pdf-generation are going perfect. The reason for the slow performance was a memory leak caused by an illplaced localization function. Sorry and thank you your fine plugin.