native_pdf_engine 0.0.3
native_pdf_engine: ^0.0.3 copied to clipboard
A high-performance, FFI-based Flutter package to convert HTML and URLs to PDF using native OS webviews (Android, iOS, macOS, Windows, Linux).
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 | Testing Note |
|---|---|---|---|
| Android | Android WebView | ✅ | Tested on Real Device |
| iOS | WKWebView | ✅ | Tested on Real Device |
| macOS | WKWebView | ✅ | Tested on Real Device |
| Windows | WebView2 (Chromium) | ✅ | Tested via Integration Tests |
| Linux | WebKitGTK | ✅ | Tested via Integration Tests |
Note: Windows and Linux support has been verified using automated integration tests. If you encounter any issues on specific distributions or setups, please open an issue with the full error log and sample code.
Features #
- Convert HTML String to PDF: Render raw HTML content directly to a PDF file or data.
- Convert URL to PDF: Capture a full webpage and save it as a PDF file or data.
- 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');
}
// 3. Get PDF Data directly (HTML)
try {
final pdfData = await NativePdf.convertToData('<h1>Direct Data</h1>');
print('Got PDF Data: ${pdfData.length} bytes');
} catch (e) {
print('Error: $e');
}
// 4. Get PDF Data directly (URL)
try {
final pdfData = await NativePdf.convertUrlToData('https://dart.dev');
print('Got PDF Data: ${pdfData.length} bytes');
} 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