Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android将webview转换为PDF_Java_Android_Pdf_Webview - Fatal编程技术网

Java Android将webview转换为PDF

Java Android将webview转换为PDF,java,android,pdf,webview,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

我正在尝试将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包。有什么想法吗?谢谢

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

或者

有一个很好用的lib

使用示例:

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

            }
        });

正如在android.print?@hyperfkcb下将PdfPrint中的print()移动到新文件中一样,正确,但我更喜欢使用我提到的lib
ConvertWebViewToPdfDemo
更可靠。很抱歉,我的意思是我可以将PdfPrint类移到android.print文件夹中吗?@hyperfkcb是的,将你的
PdfPrint.java
类移到这里yeap yeap,但我在viewmodel中实现函数时,在尝试获取活动时遇到了一些问题