从其服务器URL打印pdf

时间:2015-06-04 06:47:02

标签: android pdf printing

我有一个pdf网址,并使用以下代码在webview中显示:

android:paddingTop="20dp"
android:paddingBottom="20dp"

我需要在不下载pdf的情况下打印此pdf。 在this文档中,它仅告诉如何通过在PDF画布上呈现自定义内容并发送由此创建的PDF文档进行打印来打印自定义内容。但是没有关于如何发送webview pdf进行打印的信息? 请任何人知道如何处理这个问题。 提前致谢

2 个答案:

答案 0 :(得分:1)

这对我有用

        PrintDocumentAdapter pda = new PrintDocumentAdapter() {

        @Override
        public void onWrite(PageRange[] pages, final ParcelFileDescriptor destination, CancellationSignal cancellationSignal, final WriteResultCallback callback) {

            !!THIS MUST BE RUN ASYNC!!

            InputStream input = null;
            OutputStream output = null;

            try {
                input = new URL(YOUR URL HERE).openStream();
                output = new FileOutputStream(destination.getFileDescriptor());

                byte[] buf = new byte[1024];
                int bytesRead;

                while ((bytesRead = input.read(buf)) > 0) {
                    output.write(buf, 0, bytesRead);
                }

                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

            } catch (FileNotFoundException ee) {
                //TODO Handle Exception
            } catch (Exception e) {
                //TODO Handle Exception
            } finally {
                try {
                    input.close();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {

            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }
            PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("NAME OF DOCUMENT").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
            callback.onLayoutFinished(pdi, true);
        }
    };

    PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
    printManager.print("JOB NAME", pda, null);

答案 1 :(得分:0)

也许您可以尝试使用pdf从webView捕获图片并打印此图片。您可以使用WebView.capturePicture()之类的内容。以下是原始文档http://developer.android.com/reference/android/webkit/WebView.html#capturePicture