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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation project(path: ':escposprinter')
implementation 'com.itextpdf:itextg:5.5.9'
implementation files('libs/btsdk.jar')
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
}
# ServizioStampa.java
@Override
protected void onPrintJobQueued(PrintJob printJob) {
if (printJob.isQueued()) {
printJob.start();
}
final PrintJobInfo info = printJob.getInfo();
// final File file = new File(getFilesDir(), info.getLabel() + ".pdf");
final File file = new File(getFilesDir(), info.getLabel() + ".jpg");
InputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(printJob.getDocument().getData().getFileDescriptor());
out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
// Intent printPreview = new Intent(this, MainActivity.class);
// printPreview.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// printPreview.putExtra("FILE", file.getPath());
// startActivity(printPreview);
Log.d("irulApp-ServizioStampa", "onPrintJobQueued: file.getPath()" + file.getPath());
List<Bitmap> images = renderToBitmap(getApplicationContext(), file.getPath());
Log.d("irulApp-ServizioStampa", "List<Bitmap> images " + images.size());
} catch (IOException ioe) {
}
printJob.complete();
}
public List<Bitmap> renderToBitmap(Context context, String pdfPath) {
List<Bitmap> images = new ArrayList<>();
PdfiumCore pdfiumCore = new PdfiumCore(context);
try {
File f = new File(pdfPath);
ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
final int pageCount = pdfiumCore.getPageCount(pdfDocument);
for (int i = 0; i < pageCount; i++) {
pdfiumCore.openPage(pdfDocument, i);
int width = pdfiumCore.getPageWidthPoint(pdfDocument, i);
int height = pdfiumCore.getPageHeightPoint(pdfDocument, i);
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
pdfiumCore.renderPageBitmap(pdfDocument, bmp, i, 0, 0, width, height);
images.add(bmp);
// saving image into sdcard.
File file = new File(getFilesDir(), "image" + i + ".png");
// check if file already exists, then delete it.
if (file.exists()) file.delete();
// Saving image in PNG format with 100% quality.
try {
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
Log.v("Saved Image - ", file.getAbsolutePath());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
pdfiumCore.closeDocument(pdfDocument);
} catch (Exception e) {
//todo with exception
}
return images;
}