generate PDF from Android using PrintedPdfDocument and View.draw

前端 未结 1 880
迷失自我
迷失自我 2021-02-14 11:26

I have this layout, and I would like to print everything on the screen to PDF. including all the content in the scroll view. I will also try to do this using IText later, but fo

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-14 12:05

    A work-around would be to create a bitmap the size of the view, put it in a canvas and pass to View.draw(). Then you can scale the bitmap and draw it to the pdf canvas. You could even section it onto multiple pages.

    Keep in mind, the view is measured in pixels while the pdf canvas measures in points or 1/72inch.

    I'll add some code to my answer tomorrow.

    EDIT: Here's some working code to get any view onto a pdf or to a printer:

    public void printPDF(View view) {
        PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
        printManager.print("print_any_view_job_name", new ViewPrintAdapter(this,
                findViewById(R.id.relativeLayout)), null);
    }
    
    public class ViewPrintAdapter extends PrintDocumentAdapter {
    
    private PrintedPdfDocument mDocument;
    private Context mContext;
    private View mView;
    
    public ViewPrintAdapter(Context context, View view) {
        mContext = context;
        mView = view;
    }
    
    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
                         CancellationSignal cancellationSignal,
                         LayoutResultCallback callback, Bundle extras) {
    
        mDocument = new PrintedPdfDocument(mContext, newAttributes);
    
        if (cancellationSignal.isCanceled()) {
            callback.onLayoutCancelled();
            return;
        }
    
        PrintDocumentInfo.Builder builder = new PrintDocumentInfo
                .Builder("print_output.pdf")
                .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                .setPageCount(1);
    
        PrintDocumentInfo info = builder.build();
        callback.onLayoutFinished(info, true);
    }
    
    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
                        CancellationSignal cancellationSignal,
                        WriteResultCallback callback) {
    
        // Start the page
        PdfDocument.Page page = mDocument.startPage(0);
        // Create a bitmap and put it a canvas for the view to draw to. Make it the size of the view
        Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        mView.draw(canvas);
        // create a Rect with the view's dimensions.
        Rect src = new Rect(0, 0, mView.getWidth(), mView.getHeight());
        // get the page canvas and measure it.
        Canvas pageCanvas = page.getCanvas();
        float pageWidth = pageCanvas.getWidth();
        float pageHeight = pageCanvas.getHeight();
        // how can we fit the Rect src onto this page while maintaining aspect ratio?
        float scale = Math.min(pageWidth/src.width(), pageHeight/src.height());
        float left = pageWidth / 2 - src.width() * scale / 2;
        float top = pageHeight / 2 - src.height() * scale / 2;
        float right = pageWidth / 2 + src.width() * scale / 2;
        float bottom = pageHeight / 2 + src.height() * scale / 2;
        RectF dst = new RectF(left, top, right, bottom);
    
        pageCanvas.drawBitmap(bitmap, src, dst, null);
        mDocument.finishPage(page);
    
        try {
            mDocument.writeTo(new FileOutputStream(
                    destination.getFileDescriptor()));
        } catch (IOException e) {
            callback.onWriteFailed(e.toString());
            return;
        } finally {
            mDocument.close();
            mDocument = null;
        }
        callback.onWriteFinished(new PageRange[]{new PageRange(0, 0)});
    }
    }
    

    0 讨论(0)
提交回复
热议问题