Public
Snippet $133 authored by irul

android pdf to jpg

Edited
ServizioStampa.txt
# 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;
    }