Articles in this section

How to convert Flutter SfCalendar to PDF using PDF Viewer?

In the Flutter SfCalendar feature tour page, you can convert the SfCalendar into a PDF document by using the PDF Viewer control.

STEP 1: Add the required packages in Pubspec.yaml file.

dependencies:
  syncfusion_flutter_pdf: ^19.2.47-beta
  syncfusion_flutter_calendar: ^19.2.47
  path_provider: ^2.0.2   
  open_file: ^3.2.1
  flutter:
    sdk: flutter

STEP 2: Add the following methods for converting the calendar to PDF.

Future<void> _renderCalendarPDF() async {
    PdfDocument document = PdfDocument();
    PdfPage page = document.pages.add();
    final RenderRepaintBoundary boundary =
        _globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
    final ui.Image data = await boundary.toImage(pixelRatio: 3.0);
    final ByteData? bytes =
        await data.toByteData(format: ui.ImageByteFormat.png);
    final Uint8List imageBytes =
        bytes!.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
    page.graphics
        .drawImage(PdfBitmap(imageBytes), Rect.fromLTWH(25, 50, 300, 300));
    List<int> byteData = document.save();
    document.dispose();
 
      Directory? directory = await getExternalStorageDirectory();
      String path = directory!.path;
      print(path.toString() + ' Path');
      File file = File('$path/Output.pdf');
      await file.writeAsBytes(byteData, flush: true);
      OpenFile.open('$path/Output.pdf');
  }

STEP 3: Call this method using onPressed() callback of the button.

body: Column(
  children: [
    Container(
      height: 400,
      width: 400,
      child: RepaintBoundary(
        key: _globalKey,
        child: SafeArea(child: SfCalendar()),
      ),
    ),
    Padding(
      padding: const EdgeInsets.only(top: 10.0),
      child: ElevatedButton(
        onPressed: _renderCalenPDF, child: Text('Calendar to pdf'),),
    ),
  ],
),

View the GitHub sample here

Flutter calendar to pdf gif


Conclusion

I hope you enjoyed learning about how to convert Flutter SfCalendar to PDF using PDF viewer.

You can refer to our Flutter SfCalendar featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied