-
Notifications
You must be signed in to change notification settings - Fork 62
/
PDFPrinter.java
77 lines (63 loc) · 2.34 KB
/
PDFPrinter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.pdf.generator;
import android.print.PDFConfig;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.webkit.WebView;
import org.apache.cordova.LOG;
/**
* Created by cesar on 22/01/2017.
*/
public class PDFPrinter extends PrintDocumentAdapter {
static final String APPNAME = "PDFPrinter";
private PrintDocumentAdapter mWrappedInstance = null;
private WebView webView = null;
private PrintAttributes attributes = null;
private PDFConfig config = null;
public PDFPrinter(WebView webView, String fileName) {
if (Build.VERSION.SDK_INT >= 21) {
this.mWrappedInstance = webView.createPrintDocumentAdapter(fileName);
} else {
this.mWrappedInstance = webView.createPrintDocumentAdapter();
}
config = new PDFConfig();
this.webView = webView;
}
private PrintAttributes configureAttributes(PrintAttributes attrs) {
LOG.i(APPNAME, "Adding Printer Attributes");
return new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(attrs.getResolution())
.setMinMargins(new PrintAttributes.Margins(5000,10,5000,10)).build();
}
@Override
public void onStart() {
mWrappedInstance.onStart();
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
mWrappedInstance.onLayout(
this.configureAttributes(oldAttributes),
this.configureAttributes(newAttributes),
cancellationSignal,
callback,
extras
);
}
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal,
WriteResultCallback callback) {
mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback);
}
@Override
public void onFinish() {
LOG.i(APPNAME, "Cleaning pdfwriter & webView objects.");
mWrappedInstance.onFinish();
}
}