native_pdf_renderer 5.0.0+1 icon indicating copy to clipboard operation
native_pdf_renderer: ^5.0.0+1 copied to clipboard

Flutter plugin to render PDF pages as images on Web, MacOS, Windows, Android and iOS.

Plugin renamed and republished as [pdfx] #

[pdfx] on pub.dev

Some smaller api changes

Migration:

  1. Replace dependencies
dependencies:
-   native_pdf_renderer: ^4.0.1
+   pdfx: ^1.0.0
  1. Renamed PdfPageFormat -> PdfPageImageFormat
  2. Re-case values PdfPageImageFormat{JPEG,PNG,WEBP} -> PdfPageImageFormat{jpeg,png,webp}

PDF Renderer #

Flutter Plugin to render PDF pages as images on Web, MacOs 10.11+, Android 5.0+, iOS and Windows.

We also support the package for easy display PDF documents native_pdf_view

Getting Started #

In your flutter project add the dependency:

pub package

dependencies:
  native_pdf_renderer: any

For web add lines in index.html before importing main.dart.js:<br/> note that the files have different names

<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@2.7.570/build/pdf.js" type="text/javascript"></script>
<script type="text/javascript">
  pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/pdfjs-dist@2.7.570/build/pdf.worker.min.js";
  pdfRenderOptions = {
    cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.7.570/cmaps/',
    cMapPacked: true,
  }
</script>

for windows the pdfium version used can be overridden by the base flutter application by adding the following line to the host apps CMakeLists.txt file:

set(PDFIUM_VERSION "4638" CACHE STRING "")

Usage example #

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_pdf_renderer/native_pdf_renderer.dart';

void main() async {
  try {
    final document = await PdfDocument.openAsset('assets/sample.pdf');
    final page = await document.getPage(1);
    final pageImage = await page.render(width: page.width, height: page.height);
    await page.close();
    runApp(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Image(
              image: MemoryImage(pageImage.bytes),
            ),
          ),
        ),
        color: Colors.white,
      )
    );
  } on PlatformException catch (error) {
    print(error);
  }
}

Api #

PdfDocument #

ParameterDescriptionDefault
sourceNameNeeded for toString method. Contains a method for opening a document (file, data or asset)-
idDocument unique id. Generated when opening document.-
pagesCountAll pages count in document. Starts from 1.-
isClosedIs the document closed-

Local document open:

// From assets (Android, Ios, MacOs, Web)
PdfDocument.openAsset('assets/sample.pdf')

// From file (Android, Ios, MacOs)
PdfDocument.openFile('path/to/file/on/device')

// From data (Android, Ios, MacOs, Web)
PdfDocument.openData((FutureOr<Uint8List>) data)

Network document open:

Install [network_file] package (supports all platforms):

flutter pub add internet_file

And use it

import 'package:internet_file/internet_file.dart';

PdfDocument.openData(InternetFile.get(
    'https://github.com/rbcprolabs/packages.flutter/raw/fd0c92ac83ee355255acb306251b1adfeb2f2fd6/packages/native_pdf_renderer/example/assets/sample.pdf',
))

Open page:

final page = document.getPage(pageNumber); // Starts from 1

Close document:

document.close();

PdfPage #

ParameterDescriptionDefault
documentParent documentParent
idPage unique id. Needed for rendering and closing page. Generated when opening page.-
widthPage source width in pixels, int-
heightPage source height in pixels, int-
isClosedIs the page closedfalse

Render image:

final pageImage = page.render(
  // rendered image width resolution, required
  width: page.width * 2,
  // rendered image height resolution, required
  height: page.height * 2,

  // Rendered image compression format, also can be PNG, WEBP*
  // Optional, default: PdfPageFormat.PNG
  // Web not supported
  format: PdfPageFormat.JPEG,

  // Image background fill color for JPEG
  // Optional, default '#ffffff'
  // Web not supported
  backgroundColor: '#ffffff',

  // Crop rect in image for render
  // Optional, default null
  // Web not supported
  cropRect: Rect.fromLTRB(left, top, right, bottom),
);

PdfPageImage #

ParameterDescriptionDefault
idPage unique id. Needed for rendering and closing page. Generated when render page.-
pageNumberPage number. The first page is 1.-
widthWidth of the rendered area in pixels, int-
heightHeight of the rendered area in pixels, int-
bytesRendered image result, Uint8List-
formatRendered image compression format, for web always PNGPdfPageFormat.PNG

Close page:
Before open new page android asks to close the past.
If this is not done, the application may crash with an error

page.close();

* PdfPageFormat.WEBP support only on android

Rendering additional info #

On Web #

This plugin uses the PDF.js

On Android #

This plugin uses the Android native PdfRenderer

On Ios & MacOs #

This plugin uses the IOS native CGPDFPage

On Windows #

This plugin use PDFium

95
likes
110
pub points
93%
popularity

Publisher

verified publisher iconserge.software

Flutter plugin to render PDF pages as images on Web, MacOS, Windows, Android and iOS.

View/report issues

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

flutter, pdfx

More

Packages that depend on native_pdf_renderer