使用webview将Html转换为PDF:无法创建多个页面

问题描述 投票:0回答:1

我正在处理当前应用程序中的一个功能,我想将html文件转换为pdf格式并将其保存在存储中。

因为我现在正在使用Printing HTML documents PdfDocument我不喜欢使用iText,因为他们需要你购买商业许可证。我想用webview实现这种打印。

首先,我正在努力处理图像显示和正确的显示内容,这是我通过更改我的代码和html文件来实现的,现在我无法在pdf中创建多个页面。我只能创建一个页面,其中包含非常小的图像和内容,并且只能在一个页面中创建,而不是在其他页面中潜水。

我试图遵循一些链接,但无法理解。

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

Creating PDF from WebView truncates PDF

问题:我的方法是对的!?如何更改createPdf()以便它为我提供包含多个页面的PDF。假设我的HTML很庞大,并且它不适合一个A4页面。我是否需要使用PdfDocument.Page(canvas)和startPage()以及finishPage()手动创建单独的页面?如何根据页面大小拆分WebView?有没有自动创建页面的方法?

编辑:我试图更改imgsrc,我下载并保存资产文件夹中的图像

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

我的代码如下:

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


}
android layout android-webview pdf-generation html-to-pdf
1个回答
0
投票

如果我理解你的问题,你可以使用以下回购:

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

或这个:

Create a PDF from Webview on Android

© www.soinside.com 2019 - 2024. All rights reserved.