简体   繁体   English

使用Android中的打印管理器将字节数组打印为PDF

[英]Print a byte array as a PDF using Print Manager in Android

I'm retrieving a PDF in the form of a byte array from the internet. 我正在从互联网上检索字节数组形式的PDF。 Once I recieve it, I've been able to convert it to a File using this method: 收到它后,就可以使用以下方法将其转换为File:

File dir = Environment.getExternalStorageDirectory(); 文件dir = Environment.getExternalStorageDirectory();

    File assist = new File(Environment.getExternalStorageDirectory() + "/Sample.pdf");
    try {
        InputStream fis = new FileInputStream(assist);

        long length = assist.length();
        if (length > Integer.MAX_VALUE) {
            Log.e("Print error", "Too large");
        }
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        return new File(dir, "mydemo.pdf");

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

Now that I have the file, what do I do? 现在我有了文件,该怎么办? On the official dev site: https://developer.android.com/training/printing/custom-docs.html it doesn't give specific information for this, doesn't the Print Manager have a method to handle this for us when I give it a File ? 在官方开发网站上: https : //developer.android.com/training/printing/custom-docs.html它没有为此提供具体信息,打印管理器没有一种方法可以在我给它一个File Or do I have to manually create an adapter? 还是我必须手动创建适配器? If so, how would I get the page count (which I read is obligatory) and what would I do in onWrite() ? 如果是这样,我将如何获得页数(必须阅读),以及将在onWrite()做什么?

This works for me to print it directly from its URL 这对我来说可以直接从其URL打印

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM