Free Support Forum - groupdocs.com

Issue regarding byte array to pdf conversion


#1

Hi Team,

My requirement is that we have a byte[] (byte array) and i want it to convert pdf but io am not getting such code that fulfill my requirement. Please help to provide me sample code for byte array to pdf conversion.


#2

@saras1234,

Please note that API provides only following document loading options:

  • Load document from Amazon S3 Storage
  • Azure Blog
  • FTP
  • Local Disc
  • Stream
  • URL

However, we have logged this scenario in our internal issue tracking system with ID: CONVERSIONJAVA-800. We’ll now further investigate it and let you know if this is possible.


#3

Thanks Team so further can you please provide the code for Stream to .pdf conversion and after that i want to download this file in java .
please assist me asap i am stuck on that


#4

@saras1234,

Please go through this documentation and this API reference.
Below is the sample code:

PdfSaveOptions saveOption = new PdfSaveOptions();
String guid = "D:/conversion/Data/SourceFiles/demo.docx";
FileInputStream fileStream = new FileInputStream(guid);
ConvertedDocument convertedDocumentPath = conversionHandler.<String> convert(fileStream,saveOption);

You have to write your own logic to download the file. API just generates output in a specified folder.


#5

@saras1234,

Please see if this can help:

InputStream inputStream = new ByteArrayInputStream(data);
ConvertedDocument convertedDocument = conversionHandler.convert(inputStream, new PdfSaveOptions());
convertedDocument.save("converted from byte array.pdf");
inputStream.close();

#6

Hi Team,

Can you please provide the code how to create ConversionHandler object.
Please tell me how to create ConversionHandler object and how to get Configuration object .

Actually i am using below code , but getting performance issue can you pls help on below code :

ConversionHandler conversionHandler = new ConversionHandler(Utilities.getConfiguration());
PdfSaveOptions saveOption = new PdfSaveOptions();
saveOption.getPdfOptions().getFormatingOptions().setPageMode(PdfFormattingOptions.PdfPageMode.FullScreen);
saveOption.getPdfOptions().getFormatingOptions().setPageLayout(PdfFormattingOptions.PdfPageLayout.SinglePage);
//saveOption.getWatermarkOptions().setColor(Color.ORANGE);
byte[] bytearr = html.getBytes();

	ConvertedDocument convertedDocument= conversionHandler.convert(new ByteArrayInputStream(bytearr),fileName,saveOption);
	GroupDocsOutputStream groupDocsOutputStream = new GroupDocsOutputStream(response.getOutputStream());
	convertedDocument.save(groupDocsOutputStream);
	
	// this is my configuration method from utilities class
	public static ConversionConfig getConfiguration(){
		//ExStart:getConfiguration
		try{
			// Setup Conversion configuration
			ConversionConfig conversionConfig = new ConversionConfig();
			return conversionConfig;
		}
		catch (Exception exp) {
			System.out.println("Exception: " + exp.getMessage());
			exp.printStackTrace();
			return null;
		}
		//ExEnd:getConfiguration
	}

#7

@saras1234,

Can you please further elaborate this?
What we understand is that you want to see how to instantiate ConversionConfig to set conversion configuration and how to create instance of ConversionHandler class to convert between different file formats, right?
If yes, please go through following resources:

See code below:

ConversionConfig conversionConfig = new ConversionConfig();
conversionConfig.setStoragePath(storagePath);
conversionConfig.setOutputPath(outputPath); 
// Instantiating the conversion handler
ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);

If you give us more details, we can assist you accordingly.


#8

Hi Team,

I don’t want to use below code bcs in my case i am only wants to download that converted pdf file so there is no need to take storagePath and outputPath
Please assist me in that case what i can do :

conversionConfig.setStoragePath(storagePath);
conversionConfig.setOutputPath(outputPath);


#9

@saras1234,

Please note that API doesn’t provide any feature to download the file. It only allows you to save the output. These are the ConversionConfig method details.

There are two options:

  • Save resultant file to output folder
  • Save result in a stream and then utilize it as per your needs (this may meet your requirements)

Please visit this article and see how to store result in a IO stream.


#10

Hi Team,

I am trying to use below code sample but getting exception

ConversionHandler conversionHandler = new ConversionHandler(new ConversionConfig());
InputStream inputStream = new ByteArrayInputStream(data);
ConvertedDocument convertedDocument = conversionHandler.convert(inputStream, new PdfSaveOptions());
GroupDocsOutputStream groupDocsOutputStream = new GroupDocsOutputStream(response.getOutputStream());
convertedDocument.save(groupDocsOutputStream);
inputStream.close();

when i execute this code below exception occured!

m.groupdocs.conversion.internal.c.a.ms.System.IO.MemoryStream.checkIfClosedThrowDisposed(Unknown Source)
_ at com.groupdocs.conversion.internal.c.a.ms.System.IO.MemoryStream.seek(Unknown Source)_
_ at com.groupdocs.conversion.domain.DocumentLoader.a(Unknown Source)_
_ at com.groupdocs.conversion.domain.DocumentLoader.load(Unknown Source)_
_ at com.groupdocs.conversion.domain.DocumentLoader.load(Unknown Source)_
_ at com.groupdocs.conversion.handler.ConversionHandler.a(Unknown Source)_
_ at com.groupdocs.conversion.handler.ConversionHandler.a(Unknown Source)_
_ at com.groupdocs.conversion.handler.ConversionHandler.a(Unknown Source)_
_ at com.groupdocs.conversion.handler.ConversionHandler.convert(Unknown Source)_
_ at com.sdgc.truops.policy.document.CommonConversion.convertToPdf(CommonConversion.java:65)_

I think i am not set proper implementation of ConversionConfig bcs i called it without setting any path bcs i no need to set storage path, cache path etc


#11

@saras1234,

We are investigating this. You’ll be notified about the outcomes soon.


#12

@saras1234,

Please tell which API version (e.g. 10.6, 19.10) you are using? Make sure data size is greater than 0.
Also, can you please share a simple console application using that we can reproduce the issue?


#13

Hi Team,

I am using below dependency

com.groupdocs
groupdocs-conversion
19.4

My Sample code example given below !

public void convertToPdf(String policyId,Integer documentId,ServletContext context,ServletOutputStream outputStream,HttpServletResponse response, String pageFlag) throws UnsupportedEncodingException, Exception {
** Utilities.applyLicenseFromFile();**
** String fileName = “htmlToPdfTest1.html”;**
** PdfSaveOptions saveOption = new PdfSaveOptions();**
** saveOption.getPdfOptions().getFormatingOptions().setPageMode(PdfFormattingOptions.PdfPageMode.FullScreen);**
** saveOption.getPdfOptions().getFormatingOptions().setPageLayout(PdfFormattingOptions.PdfPageLayout.SinglePage);**
** String html=“

This article is about the founder of Microsoft. For other uses, see <a href=“https://en.wikipedia.org/wiki/Bill_Gates_(disambiguation)”>Bill Gates (d

<figure class=“table”><th colspan=“2”>

Bill Gates

<a href=“https://en.wikipedia.org/wiki/Knight_Commander_of_the_Order_of_the_British_Empire”>KBE

<th colspan=“2”>

 

Gates in 2014

<th colspan=“2”>Signature<th colspan=“2”><figure class=“image”><img src=“https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Bill_Gates_signature.svg/150px-Bill_Gates_signature.svg.png” alt=“Gates’ signature” srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/34/Bill_Gates_signature.svg/225px-Bill_Gates_signature.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/34/Bill_Gates_signature.svg/300px-Bill_Gates_signature.svg.png 2x" sizes=“100vw” width=“150”>
Born

William Henry Gates III

October 28, 1955 (age 64)
 

<a href=“https://en.wikipedia.org/wiki/Seattle”>Seattle, <a href=“https://en.wikipedia.org/wiki/Washington_(state)”>Washington, U.S.

Residence ”<a href=“https://en.wikipedia.org/wiki/Bill_Gates%27s_house”>Xanadu 2.0", <a href=“https://en.wikipedia.org/wiki/Medina,Washington">Medina, Washington
Occupation
  • Software developer
  • entrepreneur
  • philanthropist
Known for Co-founder and former head of <a href=“https://en.wikipedia.org/wiki/Microsoft”>Microsoft
Net worth US$107.4 billion (November 2019)<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-Forbes_profile-1”>[1]
Title
  • Co-<a href=“https://en.wikipedia.org/wiki/Chairman”>chairman of the <a href="https://en.wikipedia.org/wiki/Bill%26_Melinda_Gates_Foundation”>Bill & Melinda Gates Foundation
  • Chairman of <a href=“https://en.wikipedia.org/wiki/Branded_Entertainment_Network”>Branded Entertainment Network
  • Chairman of <a href=“https://en.wikipedia.org/wiki/TerraPower”>TerraPower
  • Chairman of <a href=“https://en.wikipedia.org/wiki/Cascade_Investment”>Cascade Investment
  • <a href=“https://en.wikipedia.org/wiki/Technical_advisor”>Technology advisor at Microsoft
Board member of
  • Microsoft Corporation
  • <a href=“https://en.wikipedia.org/wiki/Berkshire_Hathaway”>Berkshire Hathaway
Spouse(s) <a href=“https://en.wikipedia.org/wiki/Melinda_Gates”>Melinda Gates (m.1994)
Children 3
Parent(s)
  • <a href=“https://en.wikipedia.org/wiki/William_Henry_Gates_Sr.”>William Henry Gates Sr.
  • <a href=“https://en.wikipedia.org/wiki/Mary_Maxwell_Gates”>Mary Maxwell Gates
Website <a href=“https://www.gatesnotes.com/”>gatesnotes.com

William Henry Gates III (born October 28, 1955) is an American <a href=“https://en.wikipedia.org/wiki/Business_magnate”>business magnate, software developer, investor, and philanthropist. He is best known as the co-founder of <a href=“https://en.wikipedia.org/wiki/Microsoft”>Microsoft Corporation.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-FOOTNOTEManes199411-2”>[2]<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-3”>[3] During his career at Microsoft, Gates held the positions of <a href=“https://en.wikipedia.org/wiki/Chairman”>chairman, <a href=“https://en.wikipedia.org/wiki/Chief_executive_officer”>chief executive officer (CEO), <a href=“https://en.wikipedia.org/wiki/President_(corporate_title)”>president and <a href=“https://en.wikipedia.org/wiki/Software_architect”>chief software architect, while also being the largest individual <a href=“https://en.wikipedia.org/wiki/Shareholder”>shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the <a href=“https://en.wikipedia.org/wiki/Home_computer”>microcomputer revolution of the 1970s and 1980s.

Born and raised in <a href=“https://en.wikipedia.org/wiki/Seattle,_Washington”>Seattle, Washington, Gates launched Microsoft with childhood friend <a href=“https://en.wikipedia.org/wiki/Paul_Allen”>Paul Allen in 1975 in <a href=“https://en.wikipedia.org/wiki/Albuquerque,New_Mexico">Albuquerque, New Mexico; it went on to become the world’s largest <a href=“https://en.wikipedia.org/wiki/Personal_computer”>personal computer software company.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-CNN_Tech-4”>[4]<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-7”>[a] Gates led the company as chairman and CEO until stepping down as CEO in January 2000, but he remained chairman and became chief software architect.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-8”>[7] In June 2006, Gates announced that he would be transitioning to a part-time role at Microsoft and full-time work at the <a href="https://en.wikipedia.org/wiki/Bill%26_Melinda_Gates_Foundation”>Bill & Melinda Gates Foundation, the private charitable foundation that he and his wife, <a href=“https://en.wikipedia.org/wiki/Melinda_Gates”>Melinda Gates, established in 2000.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-9”>[8] He gradually transferred his duties to <a href=“https://en.wikipedia.org/wiki/Ray_Ozzie”>Ray Ozzie and <a href=“https://en.wikipedia.org/wiki/Craig_Mundie”>Craig Mundie.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-:1-10”>[9] He stepped down as chairman of Microsoft in February 2014 and assumed a new post as technology adviser to support the newly appointed CEO <a href=“https://en.wikipedia.org/wiki/Satya_Nadella”>Satya Nadella.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-:2-11”>[10]

Through his tenure at Microsoft in the 1990s, Gates had been <a href=“https://en.wikipedia.org/wiki/Criticism_of_Microsoft”>criticized for his business tactics, which have been considered <a href=“https://en.wikipedia.org/wiki/Anti-competitive_practices”>anti-competitive. This opinion has been upheld by numerous court rulings.<a href=“https://en.wikipedia.org/wiki/Bill_Gates#cite_note-FOOTNOTELesinski200696Manes1994459-12”>[11]

"; **
** byte[] bytearr = html.getBytes();**
** ConversionConfig conversionConfig = new ConversionConfig();**
** ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);**
** InputStream inputStream = new ByteArrayInputStream(bytearr);**
** ConvertedDocument convertedDocument = conversionHandler.convert(inputStream,fileName, new PdfSaveOptions());**
** System.out.println(convertedDocument.getElapsed());**
** convertedDocument.save(“htmlToPdf.pdf”);**
** inputStream.close();**
** }**

#14

@saras1234,

We are not able to reproduce this issue using API version 19.10 and code below.

ConversionConfig conversionConfig = new ConversionConfig(); 
conversionConfig.setOutputPath(outputPath);
ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);
String html = "This article is about the founder of Microsoft. For other uses, see Bill Gates (disambiguation).William Henry Gates III (born October 28, 1955) is an American business magnate, software developer, investor, and philanthropist. He is best known as the co-founder of Microsoft Corporation.[2][3] During his career at Microsoft, Gates held the positions of chairman, chief executive officer (CEO), president and chief software architect, while also being the largest individual shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the microcomputer revolution of the 1970s and 1980s.";
PdfSaveOptions saveOption = new PdfSaveOptions(); 
saveOption.getPdfOptions().getFormatingOptions().setPageMode(PdfFormattingOptions.PdfPageMode.FullScreen); 
saveOption.getPdfOptions().getFormatingOptions().setPageLayout(PdfFormattingOptions.PdfPageLayout.SinglePage); 
byte[] bytearr = html.getBytes();
InputStream inputStream = new ByteArrayInputStream(bytearr); 
ConvertedDocument convertedDocumentPath = conversionHandler.convert(inputStream,saveOption);
convertedDocumentPath.save("output" + convertedDocumentPath.getFileType());

For the above code, we are getting this output.pdf (23.7 KB).