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(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;
}
}
В основном действии у меня есть JavascriptInterface для вызова задания на печать.
Как открыть или сохранить PDF-документ после завершения задания на печать?
private static final String TAG = PdfPrint.class.getSimpleName(); private final PrintAttributes printAttributes;
public PdfPrint(PrintAttributes printAttributes) { this.printAttributes = printAttributes; }
public void print(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; } } [/code] В основном действии у меня есть JavascriptInterface для вызова задания на печать. Как открыть или сохранить PDF-документ после завершения задания на печать? [code]@JavascriptInterface fun pdf() { val webView: WebView = findViewById(R.id.webView) webView.post(Runnable { 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 = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "/PDFTest/")
val pdfPrint = PdfPrint(attributes) pdfPrint.print(webView.createPrintDocumentAdapter(jobName), path, "output.pdf")
Log.i("pdf", "pdf created")
} catch (e: Exception) { Log.e("pdf", " pdf failed e.localizedMessage") } }) } [/code] Когда я запускаю его, я получаю только результат LOG о том, что PDF-файл был создан.