B4A Library [B4X][B4XLIB] WebViewPdfExport

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
Usage::
Dim pdfExport As WebViewPdfExport
pdfExport.Initialize(myWebView)
Wait For (pdfExport.ExportAndShare("Report.pdf", "Share Report")) Complete (success As Boolean)
or export without the share sheet
Save directly to device:
Wait For (pdfExport.ExportToFile("Report.pdf")) Complete (success As Boolean)
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:
  • Android Main.bas:
    #AdditionalJar: PdfPrint.jar
  • Add to Manifest Editor
  • 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" />
    )
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.
 

Attachments

  • WebViewPdfExport.b4xlib
    4.7 KB · Views: 5
  • PdfPrint.zip
    5.8 KB · Views: 5
  • WebViewPDFExample.zip
    66.9 KB · Views: 5
Top