Free Support Forum - groupdocs.com

How to load document stream and convert to PDF in Java

Hi @Atir_Tahir
May I know how to do this file conversion to stream in java for 20.10.3 version

My requirement would be source stream which has to be converted to pdf stream…
Can you help me on this


This Topic is created by Atir_Tahir using Email to Topic tool.

@manojaccolite

Please have a look at this documentation article - Load documents from stream. Furthermore, this GitHub example project or the code below:

String outputFolder = "D:\\";
String outputFileTemplate = new File(outputFolder, "outputpdf.pdf").getPath();
try(FileOutputStream getPageStream = new FileOutputStream(String.format(outputFileTemplate, 1))) {
      Converter converter = new Converter("D:\\sample.docx");
      PdfConvertOptions options = new PdfConvertOptions();
      converter.convert(getPageStream, options);
} catch (IOException e) {
      System.out.println(e.getMessage());
}
1 Like

Hi @Atir_Tahir
Thanks for the reply. I have already went through the article. I’m aware of how to read a file via stream (as mentioned in the article) but getting the output file(pdf file) as a stream is what I’m bothered about. Could you plz assist me on that. Rather than providing a output folder/directory I need the converter to provide me an InputStream object. I dont want to create a file after converting it to pdf. I would need the stream after pdf conversion

@manojaccolite

We are investigating this scenario with ID CONVERSIONJAVA-899. You’ll be notified as there’s any update.

@Atir_Tahir

Thanks. Will the previous versions of groupdocs do this job??

1 Like

@manojaccolite

We’d recommend you to use latest version. So that you could get future updates continuously. Below is the code to get output in stream:

try (FileOutputStream fileOutputStream  = new FileOutputStream("output.jpg")) {
    converter.convert(fileOutputStream, options);
}

Or

try (ByteArrayOutputStream byteArrayOutputStream  = new ByteArrayOutputStream()) {
   converter.convert(byteArrayOutputStream, options);
}

Have a look at this API reference guide.

1 Like

@Atir_Tahir
It was really helpful. Thanks a lot.

I have one more concern on svg, txt, eml format. Below is the method I’m using to convert some formats which includes svg , txt, eml.

private static InputStream convertToPdf(InputStream srcFileStream, String fileName, String fileExtension) throws Exception{
        Converter converter = new Converter(srcFileStream);
        PdfConvertOptions options = new PdfConvertOptions();
        ByteArrayOutputStream byteArrayOutputStream  = new ByteArrayOutputStream();
        converter.convert(byteArrayOutputStream, options);
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    } 

For the files with svg, txt and eml extension. I’m getting the following issue;

Exception in thread “main” java.lang.NoClassDefFoundError: com/groupdocs/conversion/internal/o/a/c/compress/archivers/ArchiveStreamFactory
at com.groupdocs.conversion.internal.c.g.f.adapters.b.r(Unknown Source)
at com.groupdocs.conversion.internal.c.g.f.adapters.b.f(Unknown Source)
at com.groupdocs.conversion.internal.c.g.f.adapters.b.a(Unknown Source)
at com.groupdocs.conversion.internal.c.g.f.adapters.b.a(Unknown Source)
at com.groupdocs.conversion.filetypes.FileType.fromStream(Unknown Source)
at com.groupdocs.conversion.pipeline.a.a(Unknown Source)
at com.groupdocs.conversion.Converter.(Unknown Source)
at com.groupdocs.conversion.Converter.(Unknown Source)
at com.example.docstore.DocStoreMainApplication.convertToPdf(DocStoreMainApplication.java:63)
at com.example.docstore.DocStoreMainApplication.convertFileToPdf(DocStoreMainApplication.java:83)
at com.example.docstore.DocStoreMainApplication.main(DocStoreMainApplication.java:102)
Caused by: java.lang.ClassNotFoundException: com.groupdocs.conversion.internal.o.a.c.compress.archivers.ArchiveStreamFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
… 11 more

But when I change the code like below, it starts to work

Converter converter = new Converter("src/main/resources/input/Angular.txt");

Its only taking the src path, else I’m getting an exception like above for the highlighted formats. Can you help me on this…

1 Like

@manojaccolite

Can you please share a console application using that issue could be reproduced? Also share the problematic files.

1 Like

@Atir_Tahir

Thanks for your help.
I have uploaded a zip version of the project. Kindly extract and import it as a maven project. You will find the files under src/main/resources/input directory

After installing the dependencies. You will get to run the application.

@manojaccolite

Thank you for the details. This issue is reproduced at our end. Hence, we’ve logged it in our internal issue tracking system with ID CONVERSIONJAVA-1189. You’ll be notified as there’s any update.

1 Like

Thanks @Atir_Tahir. Kindly let me know asap as based on this my team would opt for premium plan in groupdocs. Again, Thanks a lot for your assistance.

1 Like

@manojaccolite

We’ll notify you as there’s any update.