Android将Webview转换为PDF

时间:2018-07-02 01:34:29

标签: java android pdf webview

我正在尝试将Android中的Webview转换为pdf文件。这是当我的按钮处于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");
}

还有PdfPrint类:

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;
}
} 

但是,我在PrintDocumentAdapter.WriteResultCallback()上收到了编译错误消息

错误消息是

  

WriteResultCallback在以下位置不公开   android.print.PrintDocumentAdapter.WriteResultCallback。不可能是   从外部程序包访问。

问题是我无法找到android.print包。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试在src文件夹中创建一个名称为android.print的软件包。然后使用“打印”方法在此处创建文件。

或者

有一个ConvertWebViewToPdfDemo这个库,工作得很好。

使用示例:

        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();

            }
        });