You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When splitting a PDF into individual pages using the Syncfusion PDF library in Flutter, the output pages are truncated or incomplete. This issue occurs regardless of the PDF size or complexity and affects the integrity of the resulting pages.
Steps to reproduce
Set up a Flutter project and include the Syncfusion PDF library (syncfusion_flutter_pdf).
Implement a workflow to split a multi-page PDF into individual pages using the createTemplate method.
Use the following code to load a PDF, split it into individual pages, and save them:
Load the resulting split pages using a PDF viewer (e.g., Syncfusion PDF Viewer or similar tools).
Code sample
Code sample
import'dart:io';
import'package:flutter/material.dart';
import'package:syncfusion_flutter_pdf/pdf.dart';
import'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import'package:file_picker/file_picker.dart';
import'package:path_provider/path_provider.dart';
classPdfSamplePageextendsStatefulWidget {
constPdfSamplePage({Key? key}) :super(key: key);
@overrideState<PdfSamplePage> createState() =>_PdfSamplePageState();
}
class_PdfSamplePageStateextendsState<PdfSamplePage> {
List<String> _splitPagePaths = [];
Future<void> _pickAndSplitPdf() async {
try {
FilePickerResult? result =awaitFilePicker.platform.pickFiles(
type:FileType.custom,
allowedExtensions: ['pdf'], // Restrict to PDF files
);
if (result !=null&& result.files.single.path !=null) {
File pdfFile =File(result.files.single.path!);
// Split the PDF into pagesList<File> splitFiles =await_splitPdfIntoPages(pdfFile);
setState(() {
_splitPagePaths = splitFiles.map((file) => file.path).toList();
});
} else {
ScaffoldMessenger.of(context).showSnackBar(
constSnackBar(content:Text('No file selected')),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content:Text('Error picking or splitting file: $e')),
);
}
}
Future<List<File>> _splitPdfIntoPages(File pdfFile) async {
final pdfBytes =await pdfFile.readAsBytes();
final document =PdfDocument(inputBytes: pdfBytes);
List<File> pageFiles = [];
final appDir =awaitgetApplicationDocumentsDirectory();
for (int i =0; i < document.pages.count; i++) {
final page =PdfDocument();
page.pages.add().graphics.drawPdfTemplate(
document.pages[i].createTemplate(),
Offset(0, 0),
);
final pageBytes = page.saveSync();
final pageFile =File('${appDir.path}/page_${i + 1}.pdf');
await pageFile.writeAsBytes(pageBytes);
pageFiles.add(pageFile);
page.dispose();
}
document.dispose();
return pageFiles;
}
@overrideWidgetbuild(BuildContext context) {
returnScaffold(
appBar:AppBar(
title:constText('PDF Split and Viewer'),
),
body:Column(
children: [
ElevatedButton(
onPressed: _pickAndSplitPdf,
child:constText('Pick and Split PDF File'),
),
constSizedBox(height:10),
Expanded(
child: _splitPagePaths.isNotEmpty
?ListView.builder(
itemCount: _splitPagePaths.length,
itemBuilder: (context, index) {
returnPadding(
padding:constEdgeInsets.symmetric(vertical:8.0),
child:SizedBox(
height:400, // Fixed height for each page preview
child:SfPdfViewer.file(
File(_splitPagePaths[index]),
pageSpacing:0,
enableDoubleTapZooming:false,
enableTextSelection:false,
pageLayoutMode:PdfPageLayoutMode.single,
canShowPaginationDialog:false,
canShowScrollHead:false,
canShowScrollStatus:false,
initialZoomLevel:1.0,
),
),
);
},
)
:constCenter(
child:Text('No PDF selected or split yet.'),
),
),
],
),
);
}
}
Screenshots or Video
Screenshots / Video demonstration
The margin ones are split PDFs and without margin are original pages.
Stack Traces
No specific stack trace, but the issue is evident in the truncated output files.
On which target platforms have you observed this bug?
Android, iOS
Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.27.0, on macOS 15.2 24C101 darwin-arm64, locale en-AU) • Flutter version 3.27.0 on channel stable at /Users/<username>/Developer/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 8495dee1fd (4 weeks ago), 2024-12-10 14:23:39 -0800 • Engine revision 83bacfc525 • Dart version 3.6.0 • DevTools version 2.40.2[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) • Android SDK at /Users/<username>/Library/Android/sdk • Platform android-35, build-tools 35.0.0 • ANDROID_HOME = /Users/<username>/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11) • All Android licenses accepted.[✓] Xcode - develop for iOS and macOS (Xcode 16.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 16C5032a • CocoaPods version 1.16.2[✓] Android Studio (version 2024.2) • Android Studio at /Applications/Android Studio.app/Contents • 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 • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11)[✓] VS Code (version 1.92.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.102.0[✓] Connected device (2 available) • iPhone (mobile) • <device-id> • ios • iOS 17.7.1 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.2[✓] Network resources • All expected network resources are available.• No issues found!
The text was updated successfully, but these errors were encountered:
`Future<List> _splitPdfIntoPages(File pdfFile) async {
final pdfBytes = await pdfFile.readAsBytes();
final document = PdfDocument(inputBytes: pdfBytes);
List pageFiles = [];
final appDir = await getApplicationDocumentsDirectory();
for (int i = 0; i < document.pages.count; i++) {
final originalPage = document.pages[i];
final originalSize = originalPage.size; // Get original page size (width, height)
final newDocument = PdfDocument();
// Explicitly set margins to zero
newDocument.pageSettings.margins.all = 0;
final newPage = newDocument.pages.add();
final graphics = newPage.graphics;
// Draw the content of the original page to the new page
graphics.drawPdfTemplate(
originalPage.createTemplate(),
Offset.zero, // No offset
);
// Save the new page as a file
final pageBytes = newDocument.saveSync();
final pageFile = File('${appDir.path}/page_${i + 1}.pdf');
await pageFile.writeAsBytes(pageBytes);
pageFiles.add(pageFile);
newDocument.dispose();
Bug description
When splitting a PDF into individual pages using the Syncfusion PDF library in Flutter, the output pages are truncated or incomplete. This issue occurs regardless of the PDF size or complexity and affects the integrity of the resulting pages.
Steps to reproduce
Set up a Flutter project and include the Syncfusion PDF library (syncfusion_flutter_pdf).
Implement a workflow to split a multi-page PDF into individual pages using the createTemplate method.
Use the following code to load a PDF, split it into individual pages, and save them:
Load the resulting split pages using a PDF viewer (e.g., Syncfusion PDF Viewer or similar tools).
Code sample
Code sample
Screenshots or Video
Screenshots / Video demonstration
The margin ones are split PDFs and without margin are original pages.
Stack Traces
No specific stack trace, but the issue is evident in the truncated output files.
On which target platforms have you observed this bug?
Android, iOS
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: