Convert Html into PDF using webview : can not create multiple pages

烈酒焚心 提交于 2019-11-26 12:48:49

问题


I\'m working on one functionality in my current application, in which i want to convert html file into pdf format and save it in storage.

for that i\'m using Printing HTML documents PdfDocument right now I am not keen in using iText because they need you to buy a commercial license. i want to achieve this printing with webview.

first i\'m struggling with image display and proper display content which i achieve by changing my code and html file now i cannot create multiple pages in pdf. i can create only one page with very small images and content in size and all in one page instead of diving in other pages.

i tried to follow some link but couldn\'t understand much.

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

Creating PDF from WebView truncates PDF

Question: my approach is right!? How do I alter createPdf() so that it gives me a PDF with multiple pages. Suppose my HTML is huge and it doesn\'t fit in one A4 page. Do I need to manually create separate pages using PdfDocument.Page (canvas) and startPage() and finishPage()? How would I split the WebView based on page size? Is there any automatic way of creating pages?

Edit: i tried to change imgsrc, i download and save image in assets folder

 img src=\"imagesharveyspecter.png\" alt=\" \"
    mWebView.loadDataWithBaseURL(\"file:///android_asset/\", htmlDocument, \"text/HTML\", \"UTF-8\", null);

my code is as below:

private void doWebViewPrint() {
        mWebView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(url, \"page finished loading \" + url);
                createWebPrintJob(view);
                mWebView = null;
            }
        });

        // Generate an HTML document on the fly:
        String htmlDocument = \"my html file content\";
        mWebView.loadDataWithBaseURL(null, htmlDocument, \"text/HTML\", \"UTF-8\", null);

    }

    private void createWebPrintJob(WebView webView) {
        String jobName = \"pdfDocument\";
        PrintAttributes attributes = new PrintAttributes.Builder()
                .setMediaSize(PrintAttributes.MediaSize.ISO_A1)
                .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(webView.createPrintDocumentAdapter(jobName), path, \"output_\" + System.currentTimeMillis() + \".pdf\");
    }


public class PdfPrint{

    private static final String TAG = PdfPrint.class.getSimpleName();
    PrintAttributes printAttributes;

    public PdfPrint(PrintAttributes printAttributes) {
        this.printAttributes = printAttributes;
    }

    public void print(final 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;
    }


}

回答1:


if I understand your question, you can use below repo:

https://github.com/mehrdadmmb2/richeditor-android

or this:

Create a PDF from Webview on Android



来源:https://stackoverflow.com/questions/54870164/convert-html-into-pdf-using-webview-can-not-create-multiple-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!