Android 4.4系统打印到PDF格式,而无需用户参与 [英] Android 4.4 Print to PDF without User Involvement

查看:1493
本文介绍了Android 4.4系统打印到PDF格式,而无需用户参与的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序(Android 4.4系统 - API 20)我在哪里生成HTML格式的报告。我用web视图对象来显示我的应用程序的报告。

I am working on an application (Android 4.4 -- API 20) where I am generating a report in HTML format. I use the WebView object to display the report in my app.

我希望能够做的就是这个转换成的WebView一个PDF文档。

What I would like to be able to do is convert this WebView into a pdf document.

我已经能够使用它PdfDocument和.draw上做从web视图对象的页面进行转换。我保存文件,而这一点,除了工作的结果是单页文档。有没有分页符。

I have been able to convert it using PdfDocument, and doing .draw onto the page from the WebView object. I save the file, and this works, except that the result is a single page document. There are no page breaks.

        View content = (View) webView;
        PrintAttributes pdfPrintAttrs = new PrintAttributes.Builder().
                setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
                setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
                setResolution(new Resolution("zooey", PRINT_SERVICE, 300, 300)).
                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
                build();
        PdfDocument document = new PrintedPdfDocument(mContext,pdfPrintAttrs);
        PageInfo pageInfo = new PageInfo.Builder(webView.getMeasuredWidth(), webView.getContentHeight(), 1).create();
        Page page = document.startPage(pageInfo);
        content.draw(page.getCanvas());
        document.finishPage(page);

如果我改变它,这样我使用PrintedPdfDocumet并没有指定PageInfo我只得到了web视图对象的可查看部分。

If I change it so that I use the PrintedPdfDocumet and don't specify the PageInfo I only get the viewable part of the WebView object.

        View content = (View) webView;
        PrintAttributes pdfPrintAttrs = new PrintAttributes.Builder().
                setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
                setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
                setResolution(new Resolution("zooey", PRINT_SERVICE, 300, 300)).
                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
                build();
        PrintedPdfDocument document = new PrintedPdfDocument(mContext,pdfPrintAttrs);
        Page page = document.startPage(0);
        content.draw(page.getCanvas());
        document.finishPage(page);

如果我用PrintManager并创建与createPrintDocumentAdapter web视图对象打印适配器,我可以选择为PDF格式保存选项,并生成的PDF文件有分页符,因为我在原来的网页的CSS指定

If I use the PrintManager and create a print adapter from the WebView object with createPrintDocumentAdapter, I can select the "Save as PDF" option and the resulting pdf file has the page breaks as I specify in the CSS of the original web page.

        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
        String jobName = getString(R.string.app_name) + " Report "
                + reportName;
        PrintAttributes printAttrs = new PrintAttributes.Builder().
                setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
                setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
                build();
        PrintJob printJob = printManager.print(jobName, printAdapter,
                printAttrs);

我的问题是:我可以指定我想要的PrintManager执行另存为PDF,并提供名称和所生成文件的位置,以便存在与用户的交互

My question is: can I specify that I want the PrintManager to perform a "Save as PDF" and provide the name and location of the resulting file so that there is no interaction with the user?

或者:有没有一种方法,我可以把我的WebView对象转换为PDF格式,并允许分页符

Or: Is there a way I can convert my WebView object into a PDF and allow for page breaks.

推荐答案

这可能是一个迟到的答案,但我也需要与打印框架类似的解决方案,到目前为止,我分裂的PDF文档与$ C页面低于$ C。

It might be a late answer but I was also in need of similar solution with Print Framework so far, and I splitted the Pdf Document into pages with the code below.

据我所看到的,你不能真正使web视图或PDF文档将您的PDF文件成一个聪明的方式(不切割的文本或图像)的网页。但是,我们所能做的就是在A4或Letter尺寸的比例创建页面,所以它能够适用于打印出来的纸张格式。

As far as I can see, you cannot really make the WebView or Pdf Document splits your pdf file into pages in a smart way (not cutting the text or image). But what we can do is to create Pages in a ratio of A4 or Letter size, so it can fit into print out paper format.

但是,我现在面临的另一个问题。在code以预期的Andr​​oid 4.4系统,但不能在以后的版本如下工作。在Android中-L的WebView仅可见部分被吸入的PDF文件,但在web视图的HTML的其余部分白色空白页。

But there is another issue I'm facing. The code below works as expected in Android 4.4 but not in later versions. In Android-L, only the visible part of WebView is drawn into Pdf File, but white blank pages for the rest of the HTML in WebView.

据<一href="http://developer.android.com/reference/android/webkit/WebView.html#enableSlowWholeDocumentDraw()"相对=nofollow>文档,

公共静态无效enableSlowWholeDocumentDraw()

public static void enableSlowWholeDocumentDraw ()

有关针对第l发布应用程序,的WebView有一个新的默认行为减少了内存占用,并通过智能地选择需要绘制的HTML文档的部分提高性能。这些优化都是透明的发展。然而,在某些情况下,一个应用程序开发人员可能希望将其禁用:

For apps targeting the L release, WebView has a new default behavior that reduces memory footprint and increases performance by intelligently choosing the portion of the HTML document that needs to be drawn. These optimizations are transparent to the developers. However, under certain circumstances, an App developer may want to disable them:

      
  • 当一个应用程序使用的OnDraw(Canvas)的做自己的绘画和访问该是页面的可见部分以外的方式的页的部分。
  •   
  • 当一个应用程序使用capturePicture()来捕获一个非常大的HTML文档。需要注意的是capturePicture是德precated API。
  •   

启用绘制完整的HTML文档有显著的性能成本。在创建任何WebViews之前,此方法应被调用。

Enabling drawing the entire HTML document has a significant performance cost. This method should be called before any WebViews are created.

我已经创建了一个错误报告 ,并评论了类似的bug报告这里,但没有反应为止。但在此之前,你可以使用下面的code。

I've created a Bug Report, and commented on a similar bug report HERE, but no response so far. But until then, you can use the code below.

/**
 * Creates a PDF Multi Page Document depending on the Ratio of Letter Size.
 * This method does not close the Document. It should be Closed after writing Pdf Document to a File.
 *
 * @return
 */
private PdfDocument createMultiPagePdfDocument(int webViewWidth, int webViewHeight) {

    /* Find the Letter Size Height depending on the Letter Size Ratio and given Page Width */
    int letterSizeHeight = getLetterSizeHeight(webViewWidth);

    PdfDocument document = new PrintedPdfDocument(getActivity(), getPrintAttributes());

    final int numberOfPages = (webViewHeight/letterSizeHeight) + 1;

    for (int i = 0; i < numberOfPages; i++) {

        int webMarginTop = i*letterSizeHeight;

        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(webViewWidth, letterSizeHeight, i+1).create();
        PdfDocument.Page page = document.startPage(pageInfo);

        /* Scale Canvas */
        page.getCanvas().translate(0, -webMarginTop);
        mWebView.draw(page.getCanvas());

        document.finishPage(page);
    }

    return document;
}


/**
 * Calculates the Letter Size Paper's Height depending on the LetterSize Dimensions and Given width.
 *
 * @param width
 * @return
 */
private int getLetterSizeHeight(int width) {
    return (int)((float)(11*width)/8.5);
}

这篇关于Android 4.4系统打印到PDF格式,而无需用户参与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆