How to print a pdf document

Hi,
Is it possible to print a document already loaded in Flutter PDF Viewer ?
Thank You.

2 Replies 1 reply marked as answer

DB Dilli Babu Nandha Gopal Syncfusion Team June 3, 2021 06:45 PM UTC

Hi Alberto Monteverdi, 
 
Greetings from syncfusion. 
 
We do not have direct support to print the PDF document, instead we can print the PDF document with the help of printing package. Kindly try the following code in your end and let us know whether it achieves your requirement. 
  1. Import the following package in your main.dart.
import 'package:printing/printing.dart'; 
 
  1. Add the following code to preview the generate PDF file. With the help of onDocumentLoaded callback, the PDF document can be passed to printing widget.
3.  import 'dart:typed_data'; 
4.  import 'package:flutter/material.dart'; 
5.  import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; 
6.  import 'package:printing/printing.dart'; 
7.  void main() {​​​​​​​​ 
8.    runApp(MaterialApp( 
9.      title: 'Syncfusion PDF Viewer Demo', 
10.     home: HomePage(), 
11.   )); 
12. }​​​​​​​​ 
13. /// Represents Homepage for Navigation 
14. class HomePage extends StatefulWidget {​​​​​​​​ 
15.   @override 
16.   _HomePage createState() => _HomePage(); 
17. }​​​​​​​​ 
18. class _HomePage extends State<HomePage> {​​​​​​​​ 
19.   final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey(); 
20.   @override 
21.   Widget build(BuildContext context) {​​​​​​​​ 
22.     return Scaffold( 
23.       appBar: AppBar( 
24.         title: const Text('Syncfusion Flutter PDF Viewer'), 
25.         actions: <Widget>[ 
26.           IconButton( 
27.             icon: const Icon( 
28.               Icons.bookmark, 
29.               color: Colors.white, 
30.             ), 
31.             onPressed: () {​​​​​​​​ 
32.               _pdfViewerKey.currentState?.openBookmarkView(); 
33.             }​​​​​​​​, 
34.           ), 
35.         ], 
36.       ), 
37.       body: SfPdfViewer.asset( 
38.         'assets/sample.pdf', 
39.         key: _pdfViewerKey, 
40.         onDocumentLoaded: (PdfDocumentLoadedDetails details) async {​​​​​​​​ 
41.           final Uint8List bytes = Uint8List.fromList(details.document.save()); 
42.           await Navigator.push<dynamic>( 
43.               context, 
44.               MaterialPageRoute<dynamic>( 
45.                   builder: (BuildContext context) => SafeArea( 
46.                         child: PdfPreview( 
47.                           build: (PdfPageFormat format) => bytes, 
48.                         ), 
49.                       ))); 
50.         }​​​​​​​​, 
51.       ), 
52.     ); 
53.   }​​​​​​​​ 
54.    } 
 
Please let us know if you have any doubts. 
 
Regards, 
Dilli babu. 



Marked as answer

AM Alberto Monteverdi June 4, 2021 04:46 AM UTC

Hi,
Thank You very much for provided example, I will try it.

Loader.
Up arrow icon