在Android中生成并打印特定大小的PDF [英] Generate and print a PDF with specific size in Android

查看:455
本文介绍了在Android中生成并打印特定大小的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android应用程序,并且想生成和打印PDF.但是我遇到了麻烦.我需要用80mm的width生成PDF,并且height可能会有所不同.

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.

我正在尝试:

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

此代码生成PDF,但使用letter size.

我在 Android开发人员媒体尺寸文档中看到"ISO_C7"介质大小是我几乎想要的大小,但是我可以更改所有的"printAttrs",而PDF的最终结果没有任何变化.

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.

是否有更好的方法来生成和打印PDF?如何使用80mm的width生成我的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.

谢谢.

推荐答案

我知道稍后.但是,我已经使用这种方法制作了自定义尺寸:( widthMils:5800,heightMils:40000).度量单位是:千分之一英寸.

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

}

这篇关于在Android中生成并打印特定大小的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆