How to apply Convert.toPdf() on image selected from image gallery?

Product: PDFTron SDK

Product Version:

How to apply Convert.toPdf() on image selected from image gallery?


In my application I am trying to combine multiple images into single PDF. All the images were selected from device image gallery. Below are the things I have tried so far.

  1. I used doc.pageCreate() and Image.create(doc.sdfDoc, bitmap) to create a new page for each image. But the issue here is the selected images can be of different resoulutions. I want to maintain same page size accorss all the pages and image should be adjusted based on its resoulution.
    Issue: Here I couldn’t create propery page size and placement of image due to different image sizes

  2. Later I found using Convert.toPdf() we can combine multiple images into a pdf without having to worry about resoultions. But I couldn’t apply this function on to gallery image as the choosen path would start with content, ex: content://xxxxx/image:3993. In the examples always Assessts folder images were used.

When I tried this, I got below exception:
Convert.toPdf(doc, File(uri.path).absolutePath)

Saved tmp path null

Please let me know how can I achive this. My requirement is choosing multiple images from gallery and generate a pdf with good quality of images, good aspect ratio and minimal file size.


Could you try the following to get the image URI and convert it?

SecondaryFileFilter documentConversionFilter = new SecondaryFileFilter(getContext(), fileUri);
documentConversion = Convert.universalConversion(documentConversionFilter, options);

Please see here for more details: DocumentConversion

Best Regards,

Hi Emallon

Thanks for your help. But Conversion seems to be a separate add-on. For our license, the add-on is not included in the package. Could you provide me any alternate solution to this?


Hi Ravi,

It is possible if you open an InputStream to get the image from the content URI and then copy it to a file.

        InputStream input =  getContentResolver().openInputStream(contentUri);
        OutputStream output = new FileOutputStream(targetFile);
        IOUtils.copy(input, output);

Once you have done this you can then convert the targetFile using Convert.toPDF().

Please let us know if this works for you.

Best Regards,