Skip to content

Instantly share code, notes, and snippets.

@brettwold
Last active January 2, 2024 03:41
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brettwold/838c092329c486b6112c8ebe94c8007e to your computer and use it in GitHub Desktop.
Save brettwold/838c092329c486b6112c8ebe94c8007e to your computer and use it in GitHub Desktop.
How to save a PDF from any Android WebView
package android.print;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import java.io.File;
public class PdfPrint {
private static final String TAG = PdfPrint.class.getSimpleName();
private final PrintAttributes printAttributes;
public PdfPrint(PrintAttributes printAttributes) {
this.printAttributes = printAttributes;
}
public void print(PrintDocumentAdapter printAdapter, final File path, final String fileName) {
printAdapter.onLayout(null, printAttributes, null, new PrintDocumentAdapter.LayoutResultCallback() {
@Override
public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
printAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFile(path, fileName), new CancellationSignal(), new PrintDocumentAdapter.WriteResultCallback() {
@Override
public void onWriteFinished(PageRange[] pages) {
super.onWriteFinished(pages);
}
});
}
}, null);
}
private ParcelFileDescriptor getOutputFile(File path, String fileName) {
if (!path.exists()) {
path.mkdirs();
}
File file = new File(path, fileName);
try {
file.createNewFile();
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
} catch (Exception e) {
Log.e(TAG, "Failed to open ParcelFileDescriptor", e);
}
return null;
}
}
@Dilipx1
Copy link

Dilipx1 commented Jul 7, 2017

it's working fine..

Thank you,

@vnlevanduoc
Copy link

How to get context file with byte array in class PdfPrint

I want to save the file to the database (byte[])

@Prabha10cs
Copy link

'WriteResultCallback()' is not public in 'android.print.PrintDocumentAdapter.WriteResultCallback'. Cannot be accessed from outside package
how to solve this problem

@Prabha10cs
Copy link

LayoutResultCallback() is not public in LayoutResultCallback; cannot be accessed from outside package how to solve this probleem

@SreenuGGK
Copy link

Create a package in your src folder as android.print and place the class inside that package. Don't copy the class to your existing packages.

@vahidsis
Copy link

vahidsis commented Jan 11, 2018

Hi, It works well for me but have a problem,
Android studio removes it automatically and i must copy it again sometimes,
explained here : https://stackoverflow.com/questions/48194648
how to prevent A.S. for delete it?

@pramodkr123
Copy link

use this library working perfect :
https://github.com/pramodkr123/ConvertWebViewToPdfDemo

@shivam123456789
Copy link

shivam123456789 commented Jan 4, 2019

error: WriteResultCallback() is not public in WriteResultCallback; cannot be accessed from outside package.
how to solve this problem?

@xihuny
Copy link

xihuny commented Mar 20, 2019

error: WriteResultCallback() is not public in WriteResultCallback; cannot be accessed from outside package.
how to solve this problem?

This class has to be in "android.print" package

@KsgFellow
Copy link

I have the problem, that the FIle that is created is empty.
Any ideas why that could happen?

@kungfurabbit
Copy link

@KsgFellow, same problem

@senthilgk93
Copy link

Api level 21 webview to print not working.Issue Display empty page Print why

@habibieamrullah
Copy link

Create a package in your src folder as android.print and place the class inside that package. Don't copy the class to your existing packages.

This worked!

@aluckens
Copy link

thank you!

@marlonngalvao
Copy link

The empty file problem is because of the webview rendering time.
The pdfPrint method finishes writing to the file even before the webView finishes rendering.
I decided to render the webview underneath, before clicking the button to call pdfPrint.
So that will allow time for the webview to render.

@earlGrantHomage
Copy link

The empty file problem is because of the webview rendering time. The pdfPrint method finishes writing to the file even before the webView finishes rendering. I decided to render the webview underneath, before clicking the button to call pdfPrint. So that will allow time for the webview to render.

@marlonngalvao do you have sample gist for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment