native_pdf_engine 0.0.1
native_pdf_engine: ^0.0.1 copied to clipboard
A high-performance, FFI-based Flutter package to convert HTML and URLs to PDF using native OS webviews (Android, iOS, macOS).
native_pdf_engine #
A minimal, high-performance Flutter package for generating PDFs from HTML or URLs using native OS webview capabilities.
This package uses dart:ffi and package:jni to invoke native APIs directly, ensuring:
- Zero bloat: No bundled browser engines (uses pre-installed OS webviews).
- High performance: Direct native calls, low overhead.
- Native fidelity: PDFs look exactly as they would when printing from Safari (iOS/macOS) or Chrome (Android).
Platforms Supported #
| Platform | Tech Stack | Status |
|---|---|---|
| iOS | WKWebView + UIPrintPageRenderer (via FFI/ObjC) |
✅ |
| macOS | WKWebView + Cocoa (via FFI/ObjC) |
✅ |
| Android | android.webkit.WebView + PdfDocument (via JNI/jnigen) |
✅ |
| Windows | Planned | 🚧 |
| Linux | Planned | 🚧 |
Features #
- Convert HTML String to PDF: Render raw HTML content directly to a PDF file.
- Convert URL to PDF: Capture a full webpage and save it as a PDF.
- Background Execution: Most operations run efficiently without blocking the main UI thread (Android uses
runOnUiThreadfor safety).
Installation #
Add native_pdf_engine to your pubspec.yaml:
dependencies:
native_pdf_engine: ^0.0.1
Setup #
Android #
- Internet Permission: If you are converting URLs, ensure your
android/app/src/main/AndroidManifest.xmlincludes:<uses-permission android:name="android.permission.INTERNET" /> - Cleartext Traffic: If you need to support HTTP URLs (not recommended), allow cleartext traffic in your manifest or network security config.
iOS / macOS #
No special setup is usually required. App Sandbox/Hardened Runtime on macOS may require allowing outgoing network connections (com.apple.security.network.client) if fetching URLs.
Usage #
import 'package:native_pdf_engine/native_pdf_engine.dart';
void main() async {
// 1. Convert HTML String
try {
await NativePdf.convert(
'<h1>Hello World</h1><p>This is a native PDF!</p>',
'output/path/document.pdf',
);
print('PDF Generated!');
} catch (e) {
print('Error: $e');
}
// 2. Convert URL
try {
await NativePdf.convertUrl(
'https://flutter.dev',
'output/path/flutter_website.pdf',
);
print('URL Captured!');
} catch (e) {
print('Error: $e');
}
}
How It Works #
This package avoids the complexity of flutter_inappwebview or printing when you just need a simple, headless PDF generation:
- Android: It spins up a headless
WebView, loads the content, waits for completion (via polling), and draws the view hierarchy onto aandroid.graphics.pdf.PdfDocumentCanvas. - iOS/macOS: It creates a headless
WKWebView, loads the content, and uses the nativecreatePDFconfiguration API available in WebKit.
License #
MIT