Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: PrintedPdfDocument Resolution has no effect

When drawing views into the PrintedPdfDocument canvas, the size in bytes of the PDF can noticeably increase, especially when the view contains bitmaps (e.g. ImageView).

One way to reduce the final size should be the resolution field in PrintAttributes, example:

PrintAttributes printAttrs = new PrintAttributes.Builder().
                setColorMode(PrintAttributes.COLOR_MODE_COLOR).
                setMediaSize(PrintAttributes.MediaSize.ISO_A4).
                setResolution(new Resolution("zooey", PRINT_SERVICE,hDpi,vDpi)).
                setMinMargins(Margins.NO_MARGINS).
                build();
PdfDocument document = new PrintedPdfDocument(this, printAttrs);

However, whatever I choose as hDpi and vDpi the PDF final size does not change at all.

Am I doing something wrong? How can I reduce the PDF size?

like image 250
vitil Avatar asked Jun 29 '26 08:06

vitil


1 Answers

Base on my experience, the Resolution setting doesn't effect the final result of PrintedPDfDocument file generation.

Below is the source code of PrintedPDfDocument constructor.

private static final int POINTS_IN_INCH = 72;

public PrintedPdfDocument(Context context, PrintAttributes attributes) {
    MediaSize mediaSize = attributes.getMediaSize();

    // Compute the size of the target canvas from the attributes.
    mPageWidth = (int) (((float) mediaSize.getWidthMils() / MILS_PER_INCH)
            * POINTS_IN_INCH);
    mPageHeight = (int) (((float) mediaSize.getHeightMils() / MILS_PER_INCH)
            * POINTS_IN_INCH);

    // Compute the content size from the attributes.
    Margins minMargins = attributes.getMinMargins();
    final int marginLeft = (int) (((float) minMargins.getLeftMils() / MILS_PER_INCH)
            * POINTS_IN_INCH);
    final int marginTop = (int) (((float) minMargins.getTopMils() / MILS_PER_INCH)
            * POINTS_IN_INCH);
    final int marginRight = (int) (((float) minMargins.getRightMils() / MILS_PER_INCH)
            * POINTS_IN_INCH);
    final int marginBottom = (int) (((float) minMargins.getBottomMils() / MILS_PER_INCH)
            * POINTS_IN_INCH);
    mContentRect = new Rect(marginLeft, marginTop, mPageWidth - marginRight,
            mPageHeight - marginBottom);
}

You can see that the code does not use the DPI parameter, it uses the 72 as the fixed DPI to calculate the page width/height, which I suppose is wrong.

I got a 1G PDF file when I tried to print 15 pages webpage on a tablet using PrintedPDfDocument API.

So my suggestion for your question is to use another PDF generation library until the PrintedPDfDocument proves itself later.

Good Luck.

like image 152
Zephyr Avatar answered Jun 30 '26 23:06

Zephyr