Articles in this section
Category / Section

How to create PDF conformance document in Syncfusion Flutter PDF Library?

7 mins read

The Flutter PDF is a Flutter library used to create, read, and edit PDF documents. Using this library, you can create PDF conformance documents such as:

  • PDF/A-1b conformance
  • PDF/A-2b conformance
  • PDF/A-3b conformance: The PDF/A-3b conformance supports the external files as an attachment to the PDF document, so you can attach any document formats such as Excel, Word, HTML, CAD, or XML files.

Steps to create a PDF conformance document programmatically

  1. Create a new Flutter application project.

1.1.Open Visual Studio Code (After installing the Dart and Flutter extensions as stated in the setup editor page)

1.2.Click View -> Command Palette…

Command Palette...

1.3.Type Flutter and choose Flutter: New Project.

Flutter - New Application Project

1.4.Enter the project name and press the Enter button.

1.5.Now, choose the location of the project.

  1. Add the following code in your pubspec.yaml file to install the syncfusion® flutter pdf package in your application. It will be automatically downloaded from the pub once you trigger the flutter pub to get a comment or the Get packages option from the Visual Studio Code.
    dependencies:
      syncfusion_flutter_pdf: ^20.3.49
    
  1. Import the following package in your main.dart file.
    import 'package:syncfusion_flutter_pdf/pdf.dart';
    
  1. Add the following code in the lib/main.dart file to create a simple button.
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextButton(
                child: Text(
                  'Generate PDF',
                  style: TextStyle(color: Colors.white),
                ),
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.resolveWith(
                        (states) => Colors.blue)),
                onPressed: createConformancePdf,
              )
            ],
          ),
        ),
      );
    }
    
  1. Add the following code to the createConformancePdf method to create a PDF conformance document programmatically.
    Future<void> createConformancePdf() async {
      //Create a new PDF document with conformance A1B.
      PdfDocument document =
          PdfDocument(conformanceLevel: PdfConformanceLevel.a1b)
            ..pages.add().graphics.drawString('Hello World!',
                PdfTrueTypeFont(await _readData('Roboto-Regular.ttf'), 12),
                brush: PdfBrushes.red, bounds: Rect.fromLTWH(0, 0, 200, 15));
      //Save and dispose the document.
      final List<int> bytes = await document.save();
      document.dispose();
      //Save the file and launch/download.
      SaveFile.saveAndLaunchFile(bytes, 'output.pdf');
    }
    
  1. Include the following code to read the font file from the folder where it is saved. Here, we have named our folder as assets.
    Future<List<int>> _readData(String name) async {
      final ByteData data = await rootBundle.load('assets/$name');
      return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    }
    
  1. Before that, mention the asset to pubspec.yaml file as follows.

 

# To add assets to your application, add an assets section, like this:
assets:
  - assets/Roboto-Regular.ttf
  1. Use the following code to save and launch the generated PDF file.

       Web:

8.1.Create a new dart file named save_file_web.dart under the lib folder and import the following packages in save_file_web.dart file.

import 'dart:convert';
import 'dart:html';

8.2.Include the following code snippet in save_file_web.dart file to open the PDF document in Web.

class SaveFile {
  static Future<void> saveAndLaunchFile(
      List<int> bytes, String fileName) async {
    AnchorElement(
        href:
            'data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}')
      ..setAttribute('download', fileName)
      ..click();
  }
}

       Desktop and Mobile:

8.3.Add the following dependencies in your pubspec.yaml file.

open_file: ^3.2.1   #Open source library to launch the PDF file in mobile devices
path_provider: ^2.0.11

 

8.4.Create a new dart file named save_file_mobile_and_desktop.dart under the lib folder and  import the following packages in save_file_mobile_and_desktop.dart file.

import 'dart:io';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';

 

8.5.Include the following code snippet in save_file_mobile_and_desktop.dart to open the PDF document in Desktop and Mobile.

class SaveFile {
   static Future<void> saveAndLaunchFile(
      List<int> bytes, String fileName) async {
    //Get external storage directory
    Directory directory = await getApplicationSupportDirectory();
    //Get directory path
    String path = directory.path;
    //Create an empty file to write PDF data
    File file = File('$path/$fileName');
    //Write PDF data
    await file.writeAsBytes(bytes, flush: true);
    //Open the PDF document in mobile
    OpenFile.open('$path/$fileName');
  }
}
  1. Run the sample using the flutter run command. This will create a PDF conformance document. After the application launches, you will get the PDF document as follows.

PDF Conformance

A complete working sample can be downloaded from conformance.zip

Take a moment to peruse the documentation. You can find other options like PDF/A-1b conformance, PDF/A-2b conformance, PDF/A-3b conformance in the PDF document, and features like adding layersinteractive annotations, and insert a hyperlink to PDF with code examples.

 

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