代码之家  ›  专栏  ›  技术社区  ›  QWERTY

Android将WebView转换为PDF

  •  1
  • QWERTY  · 技术社区  · 6 年前

    我正在尝试将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 回复  |  直到 6 年前
        1
  •  1
  •   Vikasdeep Singh    6 年前

    您可以尝试在您的 src 文件夹名称: android.print .然后用“打印”方法在那里创建一个文件。

    或者 ,

    有这个lib 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();
    
                }
            });