Generates a full-content PDF from your WebView. No print dialogs. Automatically opens the system share sheet or saves directly to the device.
Captures the entire page, not just the visible viewport. Multi-page content paginates automatically.
iOS and B4J require no extra setup.
Android's PdfPrint.jar is based on
brettwold's gist, enhanced with a completion callback for silent PDF generation.
Usage
[CODE lang="b4x" title="Usage:"]Dim pdfExport As WebViewPdfExport
pdfExport.Initialize(myWebView)
Wait For (pdfExport.ExportAndShare("Report.pdf", "Share Report")) Complete (success As Boolean)[/CODE]
or export without the share sheet
[CODE lang="b4x" title="Save directly to device"]Wait For (pdfExport.ExportToFile("Report.pdf")) Complete (success As Boolean)[/CODE]
Setup
Copy
WebViewPdfExport.b4xlib to your
B4X Additional Libraries folder.
Android only requires a couple of additional steps:
- Unzip and copy PdfPrint.jar to your B4A Additional Libraries folder
- Add to Main.bas:
- [CODE lang="b4x" title="Android Main.bas"]#AdditionalJar: PdfPrint.jar[/CODE]
- Add to Manifest Editor
- [CODE lang="b4x" title="Android mainfest"]AddApplicationText(
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$PACKAGE$.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
)
CreateResource(xml, provider_paths,
<files-path name="name" path="shared" />
)[/CODE]
Notes
B4i: The PDF is generated at your WebView's current width. If your HTML has wide tables or layouts, make sure it's responsive or keep columns to a minimum so nothing gets clipped.
B4A: Content is re-rendered at A4 width regardless of screen size, so wider layouts are handled automatically.
B4J: The HTML is saved to a file and opened in the default browser. The user can then print or save as PDF from there — PDF quality depends on the browser's print engine.