简体   繁体   English

如何在android中获取Web View的全部内容以生成PDF文件?

[英]how to get whole content of Web View to generate PDF file in android?

I'm Currently attempting to include a PDF export feature for my Android Application.我目前正在尝试为我的 Android 应用程序包含 PDF 导出功能。

This involves populating a Url, putting that into a WebView, then writing the contents of that WebView to a PDF using adapted code from here:这涉及填充一个 Url,将其放入 WebView,然后使用此处的改编代码将该 WebView 的内容写入 PDF:

public Bitmap viewToBitmap(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); public Bitmap viewToBitmap(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Canvas canvas = new Canvas(位图); view.draw(canvas);视图。绘制(画布); return bitmap;返回位图; } }

public void deleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles())
            deleteRecursive(child);
    fileOrDirectory.delete();
}

public void bmpToPdfMail(Bitmap bitmap) {
    String GVCode = String.valueOf(System.currentTimeMillis() + "_InvoiceTest");
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Invoices");
    if (myDir.exists()) {
        deleteRecursive(myDir);
    }
    myDir.mkdirs();

    String fname = GVCode + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) {
        deleteRecursive(file);
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    Document document = new Document();
    String input = Environment.getExternalStorageDirectory().toString() + "/Invoices/" + GVCode + ".jpg";
    String output = Environment.getExternalStorageDirectory().toString() + "/Invoices/" + GVCode + ".pdf";
    String stringPrintDirectory = Environment.getExternalStorageDirectory().toString() + "/Invoices/";
    File myDirPdf = new File(stringPrintDirectory);
    if (!myDirPdf.exists()) {
        myDirPdf.mkdirs();
    }
    try {
        FileOutputStream fos = new FileOutputStream(output);
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.open();
        document.open();
        Image img = Image.getInstance(input);
        document.setPageSize(img);
        document.newPage();
        img.setAbsolutePosition(0, 0);
        document.add(img);
        document.close();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

How to create PDF from WebView, which is hidden part?如何从隐藏部分的 WebView 创建 PDF?

However when the PDF is generated, I am only getting the visible area of the WebView, so information off-screen is being cut off.但是,当生成 PDF 时,我只获得了 WebView 的可见区域,因此屏幕外的信息被切断。

How to get Whole Content like (Visible or not Visible Part) in WebView to export PDF File From My Android Application...?如何在 WebView 中获取整个内容(可见或不可见部分)以从我的 Android 应用程序导出 PDF 文件...?

Try this to get bitmap, after the page is loaded successfully.在页面加载成功后,试试这个来获取位图。

public Bitmap viewToBitmap(WebView v) {
    Picture picture = v.capturePicture();
    Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
    picture.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas( b );

    picture.draw( c );
    return b;
}

Since, capturePicture() is deprecated in api level 19, Use由于capturePicture()在 api 级别 19 中已弃用,请使用

public Bitmap viewToBitmap(WebView v) {
    float scale = v.getScale(); // Used to capture whole the content
    int webViewHeight = (int)(v.getContentHeight() * scale);
    Bitmap image = Bitmap.createBitmap(v.getWidth(), webViewHeight,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    v.draw(canvas);
    return image;
}

Use bitmap to generate PDF.使用位图生成PDF。 Before that, try to view bitmap to make sure if there is a problem with pdf generation code.在此之前,尝试查看位图以确保pdf生成代码是否有问题。

I found a solution, not so elegant but working:我找到了一个解决方案,不是那么优雅但有效:

    wb.measure(View.MeasureSpec.makeMeasureSpec(
                    View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    wb.layout(0, 0, wb.getMeasuredWidth(), wb.getMeasuredHeight());
    PdfDocument document = new PdfDocument();
    int pageNumber = 1;
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(wb.getMeasuredWidth(),
            wb.getMeasuredHeight(), pageNumber).create();
    PdfDocument.Page page = document.startPage(pageInfo);
    //wb.draw(page.getCanvas());
    wb.draw(page.getCanvas());
    document.finishPage(page);

This creates a PDF page as wide as necessary to fit the whole WebView.这将创建一个与整个 WebView 所需的宽度一样宽的 PDF 页面。 Hope this help!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM