Pdfium_bindings - This project aims to wrap the complete Pdfium API in dart, over FFI

Overview

pub package

pdfium_bindings

This project aims to wrap the complete Pdfium API in dart, over FFI.

Example low level:

import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:pdfium_bindings/pdfium_bindings.dart';
import 'package:path/path.dart' as path;
import 'package:pdfium_bindings/src/utils.dart';
import 'package:image/image.dart';

void main() {
  //the dynamic library path  
 var stopwatch = Stopwatch()..start();
  var libraryPath = path.join(Directory.current.path, 'pdfium.dll');
  final dylib = DynamicLibrary.open(libraryPath);
  var pdfium = PDFiumBindings(dylib);

  var allocate = calloc;

  final config = allocate<FPDF_LIBRARY_CONFIG>();
  config.ref.version = 2;
  config.ref.m_pUserFontPaths = nullptr;
  config.ref.m_pIsolate = nullptr;
  config.ref.m_v8EmbedderSlot = 0;
  pdfium.FPDF_InitLibraryWithConfig(config);

  var filePathP = stringToNativeInt8('1417.pdf');

  var doc = pdfium.FPDF_LoadDocument(filePathP, nullptr);
  if (doc == nullptr) {
    var err = pdfium.FPDF_GetLastError();
    throw PdfiumException.fromErrorCode(err);
  }

  var pageCount = pdfium.FPDF_GetPageCount(doc);
  print('pageCount: $pageCount');

  var page = pdfium.FPDF_LoadPage(doc, 0);
  if (page == nullptr) {
    var err = pdfium.getLastErrorMessage();
    pdfium.FPDF_CloseDocument(doc);
    throw PageException(message: err);
  }

  var scale = 1;
  var width = (pdfium.FPDF_GetPageWidth(page) * scale).round();
  var height = (pdfium.FPDF_GetPageHeight(page) * scale).round();

  print('page Width: $width');
  print('page Height: $height');

  // var backgroundStr = "FFFFFFFF"; // as int 268435455
  var background = 268435455;
  var start_x = 0;
  var size_x = width;
  var start_y = 0;
  var size_y = height;
  var rotate = 0;

  // Create empty bitmap and render page onto it
  // The bitmap always uses 4 bytes per pixel. The first byte is always
  // double word aligned.
  // The byte order is BGRx (the last byte unused if no alpha channel) or
  // BGRA. flags FPDF_ANNOT | FPDF_LCD_TEXT

  var bitmap = pdfium.FPDFBitmap_Create(width, height, 0);
  pdfium.FPDFBitmap_FillRect(bitmap, 0, 0, width, height, background);
  pdfium.FPDF_RenderPageBitmap(
      bitmap, page, start_x, start_y, size_x, size_y, rotate, 0);
  //  The pointer to the first byte of the bitmap buffer The data is in BGRA format
  var pointer = pdfium.FPDFBitmap_GetBuffer(bitmap);
  //stride = width * 4 bytes per pixel BGRA
  //var stride = pdfium.FPDFBitmap_GetStride(bitmap);
  //print('stride $stride');

  var image = Image.fromBytes(
      width, height, pointer.asTypedList(width * height * 4),
      format: Format.bgra, channels: Channels.rgba);

  // save bitmap as PNG.
  File('out.png').writeAsBytesSync(encodePng(image));

  //clean
  pdfium.FPDFBitmap_Destroy(bitmap);

  pdfium.FPDF_ClosePage(page);
  allocate.free(filePathP);

  pdfium.FPDF_DestroyLibrary();
  allocate.free(config);

  print('end: ${stopwatch.elapsed}');
}

Example hi level:

void main() {
  PdfiumWrap()
      .loadDocumentFromPath('1417.pdf')
      .loadPage(0)
      .savePageAsPng('out.png')
      .closePage()
      .closeDocument()
      .dispose();
}

Example 2 hi level:

import 'package:pdfium_bindings/pdfium_bindings.dart';
import 'package:http/http.dart' as http;

void main() async {
  var pdfium = PdfiumWrap();

  var resp = await http.get(Uri.parse(
      'https://www.riodasostras.rj.gov.br/wp-content/uploads/2022/03/1426.pdf'));
  var bytes = resp.bodyBytes;

 pdfium
      .loadDocumentFromBytes(bytes)
      .loadPage(0)
      .savePageAsJpg('out.jpg', qualityJpg: 80)
      .closePage()
      .closeDocument()
      .dispose();
}

This has the potential to build a truly cross platform, high-level API for rendering and editing PDFs on all 5 platforms.

Goals

Thanks

A scientifichackers for inspiring me
A big THANK YOU to Google for open sourcing Pdfium, and releasing dart:ffi.

You might also like...

An open-source unofficial GitHub mobile client, that aims to deliver the ultimate GitHub experience on mobile devices.

An open-source unofficial GitHub mobile client, that aims to deliver the ultimate GitHub experience on mobile devices.

DioHub for Github Summary Features Roadmap Support Screenshots Build Instructions Summary DioHub is an open-source unofficial GitHub mobile client, th

Jan 4, 2023

A Flutter library aims to help you create animated, simple, and stylish Material Dialogs in your app.

A Flutter library aims to help you create animated,  simple, and stylish Material Dialogs in your app.

Flutter Material Dialogs Flutter Material Dialogs 📱 A Flutter library aims to help you create 💪🏻animated, 😃 simple, 😎 stylish Material Dialogs in

Dec 25, 2022

Song Tinder aims to ease the pain of managing song playlists

Song Tinder aims to ease the pain of managing song playlists. This is done by mainly focusing on a Tinder-like swipe card based interface which can be used for anything from sorting your existing playlists to finding new music.

Nov 17, 2022

The Health==Wealth app aims to make leading a healthy lifestyle simple, fun and rewarding for students.

The Health==Wealth app aims to make leading a healthy lifestyle simple, fun and rewarding for students. Students can also track and see their progress through the app.

Jun 25, 2022

AhoyHacks Hackathon aims to bring together developers from around the globe to build something unique on the weekend

AhoyHacks Hackathon aims to bring together developers from around the globe to build something unique on the weekend

Pirate Island Are you a budding pirate & want to go on a journey to hunt treasures? Well, what are you waiting for! Register yourself on the Pirate Is

May 15, 2022

Another breakpoint framework. Aims to simplify as much as possible building adaptive layouts.

Another breakpoint framework. Aims to simplify as much as possible building adaptive layouts. Features Really simple implementation Works with and wit

Sep 26, 2022

Socket library for creating real-time multiplayer games. Based on TCP, with the ability to send messages over UDP (planned).

Game socket The library was published in early access and is not stable, as it is being developed in parallel with other solutions. English is not a n

Aug 10, 2022

A Flutter widget for rendering static html as Flutter widgets (Will render over 80 different html tags!)

A Flutter widget for rendering static html as Flutter widgets (Will render over 80 different html tags!)

flutter_html A Flutter widget for rendering HTML and CSS as Flutter widgets. Screenshot 1 Screenshot 2 Screenshot 3 Table of Contents: Installing Curr

Jan 5, 2023

An app made using the Flutter framework that allows users to track information for over 1500 cryptocurrencies

Platypus Crypto Platypus Crypto is an ad-free cross-platform robust solution for tracking cryptocurrency assets. Our intuitive interface includes real

Jan 4, 2023
Owner
Isaque Neves
Isaque Neves
Starter project for Flutter plugins willing to access native and synchronous rust code using FFI

Flutter Rust FFI Template This project is a Flutter Plugin template. It provides out-of-the box support for cross-compiling native Rust code for all a

Jør∂¡ 561 Dec 7, 2022
A revolutionary new browser. HTML to Flutter transpiler. Written in Go (using Dart FFI) and Flutter.

Flutter Browser An experimental HTML & CSS to Flutter transpiler written in Go, using Dart FFI and Flutter. Screenshots Notice This works great for si

Mitja 12 Oct 24, 2022
A collection of Animations that aims to improve the user experience for your next flutter project.

Flutter Animations A collection of Animations that aims to improve the user experience for your next flutter project. Built by Ezaldeen SAHB I hope th

Ezaldeen Sahb 134 Dec 24, 2022
This project aims to provide a simple and customizable Review Page interaction made with Flutter.

Review Page Interaction This project aims to provide a simple and customizable Review Page interaction made with Flutter. Check it out on Béhance (htt

Marcos Morales Rodrigo 29 Mar 29, 2022
Freela is a Flutter project that aims to connect people who want to work with jobs to be done.

Freela Freela is a Flutter project being developed at the university that aims to connect people who want to work with jobs to be done. Para Desenvolv

Gustavo Martins 4 Nov 1, 2022
Aris wasmjsextend - Binding generator for FFI bindings

Binding generator for FFI bindings. Note: ffigen only supports parsing C headers

Behruz Hurramov 1 Jan 9, 2022
Flutter FFI+WebAssembly Example

flutter_ffi_plugin A new flutter plugin project. FFI # Remove Codesign on macos codesign --remove-signature /usr/local/bin/dart # Install cmake brew

Rody Davis 33 Oct 22, 2022
Codeflow 19 Sep 29, 2022
Udemy Course "Dart and Flutter: The Complete Developer's Guide" Project. (With Hive DB)

Udemy-Course-Flutter Udemy Course "Dart and Flutter: The Complete Developer's Guide" Project. (With Hive DB) The course: https://www.udemy.com/course/

Muhammad Tayyab 1 Jun 11, 2022
🚗 Apple CarPlay for Flutter Apps. Aims to make it safe to use apps made with Flutter in the car by integrating with CarPlay.

CarPlay with Flutter ?? Flutter Apps now on Apple CarPlay! flutter_carplay aims to make it safe to use iPhone apps made with Flutter in the car by int

Oğuzhan Atalay 156 Dec 26, 2022