android - 我想在没有用户交互的情况下自动打印

标签 android

当用户点击 UI 中的按钮时,PrintManager 生成要打印的文档。然后用户单击打印图标以推送打印命令。

以上部分完成。 但要求是: 我想消除用户必须单击打印图标(图片中显示的打印图标,由 PrintDocumentAdapter 生成)并自动生成/打印文档。

再次: 我已经实现了打印功能,我只想删除用户交互。当生成下图时,打印命令会自动执行,无需用户点击打印图标。

enter image description here

最佳答案

不要使用 PrintManager,您可以使用 android.print 包中的类 PdfPrint.java 进行打印。

将这段代码放在 java/android/print 中(在你的包之外)

package android.print;

import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.File;

public class PdfPrint {

    private static final String TAG = PdfPrint.class.getSimpleName();
    private final PrintAttributes printAttributes;

    public PdfPrint(PrintAttributes printAttributes) {
        this.printAttributes = printAttributes;
    }

    public void print(final PrintDocumentAdapter printAdapter, final File path, final String fileName) {
        printAdapter.onLayout(null, printAttributes, null, new PrintDocumentAdapter.LayoutResultCallback() {
            @Override
            public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
                printAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFile(path, fileName), new CancellationSignal(), new PrintDocumentAdapter.WriteResultCallback() {
                    @Override
                    public void onWriteFinished(PageRange[] pages) {
                        super.onWriteFinished(pages);
                    }
                });
            }
        }, null);
    }

    private ParcelFileDescriptor getOutputFile(File path, String fileName) {
        if (!path.exists()) {
            path.mkdirs();
        }
        File file = new File(path, fileName);
        try {
            file.createNewFile();
            return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
        } catch (Exception e) {
            Log.e(TAG, "Failed to open ParcelFileDescriptor", e);
        }
        return null;
    }
}

你可以使用这样的代码(从 webview 打印)

private fun createWebPrintJob(webView: WebView, date: Long) {
        try {
            val jobName = getString(R.string.app_name) + " Document"
            val attributes = PrintAttributes.Builder()
                .setMediaSize(PrintAttributes.MediaSize.NA_LEGAL)
                .setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
                .setMinMargins(PrintAttributes.Margins.NO_MARGINS).build()
            val path =
                Environment.getExternalStoragePublicDirectory("/pdf_output")

            val pdfPrint = PdfPrint(attributes)
            pdfPrint.print(
                webView.createPrintDocumentAdapter(jobName),
                path,
                "output_$date.pdf"
            )
            Log.i("pdf", "pdf created")
        } catch (e: Exception) {
            Log.e("pdf", " pdf failed e.localizedMessage")
        }
    }

关于android - 我想在没有用户交互的情况下自动打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48578728/

相关文章:

java - SwipeRefreshLayout + AsyncTask : Fail to refresh data

android - 在正在运行的服务上多次调用 startService()

android - 在 Android 中暂停和恢复录音

android - 如何在android中查找SQLite数据库的最后更新时间

java - Android 布局文本换行

java - LayoutInflater 膨胀 fragment

安卓运行时异常 :requestFeature() must be called before adding content in DialogFragment

android - 如何从谷歌地方检索搜索建议?

android - 在运行时显示图像和文本,android?

java - 如何在 Jersey 休息网络服务中获取图像