Generate and print a PDF with specific size in Android

我只是一个虾纸丫 提交于 2019-12-07 05:06:41

问题


I'm working in an Android application, and I want to generate and print a PDF. But I'm having some trouble. I need to generate the PDF with 80mm of width, and the height may vary.

I'm trying this:

public class PDFGenerator implements Runnable {
    private Context ctx;
    private View view;
    private Intent mShareIntent;
    private OutputStream os;

    public PDFGenerator(Context ctx, View view) {
        this.ctx = ctx;
        this.view = view;
        makeAndSharePDF();
    }

    public void makeAndSharePDF() {
        new Thread(this).start();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void run() {
        PrintAttributes printAttrs = new PrintAttributes.Builder().
                setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
                setMediaSize(PrintAttributes.MediaSize.ISO_C7).
                setResolution(new PrintAttributes.Resolution("zooey", ctx.PRINT_SERVICE, 500, 500)).
                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
                build();

        PdfDocument document = new PrintedPdfDocument(ctx, printAttrs);
        PdfDocument.PageInfo pageInfo = new  PdfDocument.PageInfo.Builder(view.getWidth(), view.getHeight(), 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);
        view.draw(page.getCanvas());
        document.finishPage(page);

        try {
            File pdfDirPath = new File(ctx.getFilesDir(), "pdfs");
            pdfDirPath.mkdirs();
            File file = new File(pdfDirPath, "danfe.pdf");
            Uri contentUri = FileProvider.getUriForFile(ctx, "br.com.rdscodes.nfcelular", file);
            os = new FileOutputStream(file);
            document.writeTo(os);
            document.close();
            os.close();
            shareDocument(contentUri);
        } catch (IOException e) {
            throw new RuntimeException("Error generating file", e);
        }
    }

    private void shareDocument(Uri uri) {
        mShareIntent = new Intent();
        mShareIntent.setAction(Intent.ACTION_SEND);
        mShareIntent.setType("application/pdf");
        mShareIntent.putExtra(Intent.EXTRA_SUBJECT, "NFC-e");
        mShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        ctx.startActivity(mShareIntent);
    }
}

This code generates the PDF, but with letter size.

I saw in Android Developers media size documentation that the "ISO_C7" media size is the size the i almost want, but i can change all the "printAttrs" and nothing changes on the final result of the PDF.

Are there better ways to generate and print a PDF? How can I generate my PDF with 80mm of width?

This part of the app was made by an old employee.

Thanks.


回答1:


I know it's a bit later. However, I have made my custom size with this approach : ( widthMils: 5800, heightMils: 40000). The unit of measurement is: one thousandth of an inch.

private PrintAttributes getDefaultPrintAttrs() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null;

    PrintAttributes.MediaSize customSize = new 
    PrintAttributes.MediaSize("COIL", "COIL", 5800,40000);
    customSize.asPortrait();

    return new PrintAttributes.Builder()
            .setMediaSize(customSize)
            .setResolution(new PrintAttributes.Resolution("RESOLUTION_ID", "RESOLUTION_ID", 600, 600))
            .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
            .build();

}



回答2:


Just so you don't break your head over it, like I did, check out this bug report: https://code.google.com/p/android/issues/detail?id=180405

Considering this bug report, I'd say it's a problem with the android printing interface and you can't really set any defaults using PrintAttributes, well at least not until android N.

If you find/found a workaround, please let me know :)



来源:https://stackoverflow.com/questions/31884342/generate-and-print-a-pdf-with-specific-size-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!