Aspose PDF - TIFF to PDF conversion

Hello,

We have a requirement to convert TIFF documents to PDF documents. With the code we have, we were able to convert many documents successfully. However, certain documents are not converting as expected and the program doesn’t fail or throw an exception. It just stops at the line doc.save and never prints anything we have after that. Could you please assist. We cannot share the TIFF document as it is a prod document.

In the code blow, it prints converting and never prints converted and no exception or error.

String tiffFilePath = “C://TEMP//Test.tiff”; // Replace with your TIFF file path

        try {
            byte[] tiffBytes = Files.readAllBytes(Paths.get(tiffFilePath));

            Document pdfDocument = new Document();
            Page page = pdfDocument.getPages().add();

            Image image = new Image();
            image.setImageStream(new java.io.ByteArrayInputStream(tiffBytes));
           // image.setImageType(ImageType.getTiff());

            page.getParagraphs().add(image);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            System.out.println("Converting.......");
            pdfDocument.save(outputStream);

            System.out.println("Converted.......");
            // The PDF is now saved in the outputStream.
            // You can further process or save it as needed.
            byte[] pdfBytes = outputStream.toByteArray();
            
            File file = new File("C://TEMP//TestPDF.pdf");
			FileOutputStream fos;

			fos = new FileOutputStream(file);
			fos.write(pdfBytes);

            System.out.println("TIFF converted to PDF and saved to ByteArrayOutputStream.");
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

@judiciary

It seems that you are experiencing an issue where the conversion of certain TIFF documents to PDF is hanging at the doc.save line without throwing any exceptions. This can be frustrating, especially when the program does not provide any feedback on what might be going wrong.

Here are a few suggestions to troubleshoot and potentially resolve the issue:

  1. Check TIFF File Compatibility: Ensure that the TIFF files you are trying to convert are not corrupted and are compatible with the Aspose.PDF library. Sometimes, specific TIFF formats or compression methods may not be supported.

  2. Use MemoryStream for Image: Instead of reading the TIFF file into a byte array and then creating a ByteArrayInputStream, consider using a MemoryStream directly. This can sometimes help with performance and compatibility issues. Here’s a modified version of your code:

    String tiffFilePath = "C://TEMP//Test.tiff"; // Replace with your TIFF file path
    
    try {
        // Load the TIFF file into a MemoryStream
        FileInputStream fileInputStream = new FileInputStream(tiffFilePath);
        MemoryStream memoryStream = new MemoryStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
            memoryStream.write(buffer, 0, bytesRead);
        }
        fileInputStream.close();
    
        Document pdfDocument = new Document();
        Page page = pdfDocument.getPages().add();
    
        Image image = new Image();
        image.setImageStream(memoryStream);
        page.getParagraphs().add(image);
    
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        System.out.println("Converting.......");
        pdfDocument.save(outputStream);
    
        System.out.println("Converted.......");
        // The PDF is now saved in the outputStream.
        byte[] pdfBytes = outputStream.toByteArray();
        
        File file = new File("C://TEMP//TestPDF.pdf");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(pdfBytes);
        fos.close();
    
        System.out.println("TIFF converted to PDF and saved to ByteArrayOutputStream.");
        outputStream.close();
    
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  3. Set Image Properties: If you are dealing with large TIFF files, consider setting properties like IsBlackWhite to true for performance improvement, as this can help reduce the processing time.

  4. Debugging: Add additional logging before and after the doc.save line to see if the program is reaching that point. This can help identify if the issue is with the save operation or something else.

  5. Review Documentation: For more detailed guidance on converting TIFF to PDF, you may want to refer to the official Aspose documentation, which provides insights into optimizing the conversion process and handling different image formats effectively.

If the problem persists after trying these suggestions, consider reaching out to Aspose support with specific details about the TIFF files you are using, as they may have additional insights or updates regarding compatibility issues.

Sources:
[1]: TIFF To PDF Performance Improvement - Aspose
[2]: Convert TIFF to PDF|Aspose.PDF for Android via Java - Aspose Documentation

Thank you for your response.

1. Check TIFF File Compatibility : We are able to open the tiff without any issues. Is there any other way to check if it is compatible with aspose?
2. Use MemoryStream for Image : Image is only taking InputStream. Memory Stream is not supported. Please confirm
3. Set Image Properties : We already have the blackwhite setting set to true
4. Debugging Like I mentioned in my sample code, we already have added a log statement before and after the doc.save. It prints the one before and no other log or exceptions get printed after that

Thanks,
B

@judiciary

Have you tried using the latest version of the API? We are afraid that we could not replicate the issue with our sample file. Please try to share the problematic TIFF file with us via private message if you cannot share it publicly. You can click on the username and press Blue Message Button to send a private message.

Hi, I have submitted another private ticket with the tiff attachment. Please let us know

@judiciary

Can you please share the link of the private post? We are unable to find it in this category.

Please find the link to the post with document attachement

https://forum.aspose.com/t/tiff-to-pdf-conversion/307990?u=judiciary

@judiciary

We are checking it and will get back to you shortly.

Hi, any update on this ticket?

Thanks,
B

@judiciary

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44757

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.