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

android– 使用webview将Html转换为PDF:无法创建多个页面,第1张

概述我正在处理当前应用程序中的一个功能,我想将html文件转换为pdf格式并将其保存在存储中.因为我正在使用PrintingHTMLdocumentsPdfDocument现在我并不热衷于使用iText,因为他们需要你购买商业许可证.我想用webview实现这种打印.首先,我正在努力处理图像显示和正确的显示内容,这

我正在处理当前应用程序中的一个功能,我想将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?有没有自动创建页面的方法?

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

我的代码如下:

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

解决方法:

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

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

或这个:

Create a PDF from Webview on Android

总结

以上是内存溢出为你收集整理的android – 使用webview将Html转换为PDF:无法创建多个页面全部内容,希望文章能够帮你解决android – 使用webview将Html转换为PDF:无法创建多个页面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103068.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存