Blodhgard
Blodhgard

Reputation: 9385

PdfDocument android 11 breaking changes

I have problems with pdf generated using PdfDocument in the new Android 10. The output file is not correct. In the other Android versions, there were no problems. Any idea?

This is how I generate each page

View contentView = pageViewList.get(i);

        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, i + 1).create();

        // Start a page
        PdfDocument.Page page = pdfDocument.startPage(pageInfo);

        // Draw view on the page
        Canvas pageCanvas = page.getCanvas();
        int pageWidth = pageCanvas.getWidth();
        int pageHeight = pageCanvas.getHeight();
        int measureWidth = View.MeasureSpec.makeMeasureSpec(pageWidth, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY);
        contentView.measure(measureWidth, measuredHeight);
        contentView.layout(0, 0, pageWidth, pageHeight);
        contentView.draw(pageCanvas);

        // Finish the page
        pdfDocument.finishPage(page);

enter image description here

Upvotes: 0

Views: 195

Answers (1)

Blodhgard
Blodhgard

Reputation: 9385

The problem seems related to TypedValue.applyDimension that return a wrong value.

    pageWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 210, displayMetrics);
    pageHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 297, displayMetrics);

So it's not related to PdfDocument.

Upvotes: 1

Related Questions