Android Question How to save a PDF file from WebView?

Riddle Ling

Member
Licensed User
How can i achieve the following code in B4A?

code source:
http://www.annalytics.co.uk/android/pdf/2017/04/06/Save-PDF-From-An-Android-WebView/

B4X:
private void createWebPrintJob(WebView webView) {
    String jobName = getString(R.string.app_name) + " Document";
    PrintAttributes attributes = new PrintAttributes.Builder()
            .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
            .setResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600))
            .setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/PDFTest/");
    PdfPrint pdfPrint = new PdfPrint(attributes);
    pdfPrint.print(webView.createPrintDocumentAdapter(jobName), path, "output_" + System.currentTimeMillis() + ".pdf");
  }
 

DonManfred

Expert
Licensed User
Longtime User
He does not want to create a pdf by code. Not in the way the lib is working...
He want to write the content of a webview to a PDF.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try this

WebWiew to Bitmap (WebView.CaptureBitmap)
Bitmap to PDF
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Yes but that link allows you to 'print to a PDF'... which is the same thing
How?
I just had a look at the methods... In the PDFDocument i did not found a way to create pages from a webview.
In the Printer i just found a way to Print the content of a webview.
I did not saw a method to print the content of a webview to a PDF.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
How?
I just had a look at the methods... In the PDFDocument i did not found a way to create pages from a webview.
In the Printer i just found a way to Print the content of a webview.
I did not saw a method to print the content of a webview to a PDF.

From the link "The Printer object can print bitmaps, html documents, WebView content and PDF documents."
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0
Top