Reputation: 2315
I am trying to convert webview in Android into pdf file. This is the function when my button is onClick:
public void export(WebView wv) {
Context context = getApplication();
String jobName = context.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(wv.createPrintDocumentAdapter(jobName), path, "output_" + System.currentTimeMillis() + ".pdf");
}
And the PdfPrint class:
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;
}
}
However, I am getting compile error message at PrintDocumentAdapter.WriteResultCallback()
The error message is
WriteResultCallback is not public in android.print.PrintDocumentAdapter.WriteResultCallback. Cannot be accessed from outside package.
The problem is I am not able to locate android.print package. Any ideas? Thanks!
Upvotes: 4
Views: 5062
Reputation: 21766
You can try by creating a package inside your src
folder with the name: android.print
. Then create a file there with your "print" method.
Alternatively,
There is this lib ConvertWebViewToPdfDemo which works very nice.
Usage sample:
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/PDFTest/");
final String fileName="Test.pdf";
final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait");
progressDialog.show();
PdfView.createWebPrintJob(MainActivity.this, webView, directory, fileName, new PdfView.Callback() {
@Override
public void success(String path) {
progressDialog.dismiss();
PdfView.openPdfFile(MainActivity.this,getString(R.string.app_name),"Do you want to open the pdf file?"+fileName,path);
}
@Override
public void failure() {
progressDialog.dismiss();
}
});
Upvotes: 2