How to print/save the flutter application screen as pdf?

import 'package:flutter/material.dart';class ScreenToPdf extends StatelessWidget {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('Screen to PDF'),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Image.network('https://picsum.photos/250?image=9',height: 200,width: 200,),const SizedBox(height: 20,),ElevatedButton(child: const Text('Screen to PDF'),onPressed: () {},),const Text('Printing the data from the screen to PDF'),],),),);}}
This is the basic layout
Widget build(BuildContext context) {
return RepaintBoundary(
key: _printKey,
child: Scaffold(...)
);}
void _printScreen() async {Printing.layoutPdf(onLayout: (PdfPageFormat format) async {final doc = pw.Document();final image = await WidgetWraper.fromKey(key: _printKey,pixelRatio: 2.0,);doc.addPage(pw.Page(pageFormat: format,build: (pw.Context context) {return pw.Center(child: pw.Expanded(child: pw.Image(image),),);}));
return doc.save();
});}
Downloaded pdf
  • This method will not work if any media component such as Youtube Player, Video player, etc are present in the widget tree as they can’t be rendered in a pdf. Make sure you are wrapping only the widget tree where any media component is not present.

--

--

--

Flutter Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building Interactive User Login Page With React.js

Diffing objects in GTM

Demystifying JavaScript Code Execution

A Gamified Approach to Learn Functional Programming

Title Card Functional Programming

how to make a responsive page by resizing the event listener with react.js

Set up a scalable and maintainable infrastructure for React secured with SSL in AWS

JS Interview Question: What’s the difference between a variable that is: null, undefined or…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shrishti Gupta

Shrishti Gupta

Flutter Developer

More from Medium

WHAT’S NEW IN FLUTTER 2.8

Programmatically Logging In to a Website with Flutter Part-1

Flutter Challenge — Flutter Puzzle Hack

Flutter: Easily add CI testing with GitHub Actions