We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Saving PDF to HTML using memory stream

Dear Aspose Team,


I’m trying to convert PDF to HTML without saving it to local drive. As far as I see there is an option to save in memory stream.
Is there any examples how to do this with examples of implementation for:
CustomStrategyOfCssUrlCreation
CustomCssSavingStrategy
CustomResourceSavingStrategy
?
I saw some examples for C# but not for Java.

Thanks,
Andrei

Hi Andrei,


Thanks for your inquiry. Yes you can save PDF to HTML results into stream using Aspose.Pdf for Java. Please check following Java code snippet to save HTML in stream with embedded resources. It will help you to accomplish the task.

Document doc = new Document(myDir+“Input.pdf”);<o:p></o:p>

// tune conversion params<o:p></o:p>

HtmlSaveOptions newOptions = new HtmlSaveOptions();<o:p></o:p>

newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;<o:p></o:p>

newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;<o:p></o:p>

newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;<o:p></o:p>

newOptions.LettersPositioningMethod = LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;<o:p></o:p>

newOptions.setSplitIntoPages(false);// force write HTMLs of all pages into one output document<o:p></o:p>

newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy() {<o:p></o:p>

<o:p></o:p>

@Override<o:p></o:p>

public void invoke(HtmlPageMarkupSavingInfo htmlSavingInfo) {<o:p></o:p>

// TODO Auto-generated method stub<o:p></o:p>

byte[] resultHtmlAsBytes = new byte[(int) htmlSavingInfo.ContentStream.getLength()];<o:p></o:p>

htmlSavingInfo.ContentStream.read(resultHtmlAsBytes, 0, resultHtmlAsBytes.length);<o:p></o:p>

// here You can use any writable stream, file stream is taken just as example<o:p></o:p>

FileOutputStream fos;<o:p></o:p>

try {<o:p></o:p>

fos = new FileOutputStream(myDir+“temp/PDFtoHTML.html”);<o:p></o:p>

fos.write(resultHtmlAsBytes);<o:p></o:p>

fos.close();<o:p></o:p>

} catch (FileNotFoundException e) {<o:p></o:p>

// TODO Auto-generated catch block<o:p></o:p>

e.printStackTrace();<o:p></o:p>

} catch (IOException e) {<o:p></o:p>

// TODO Auto-generated catch block<o:p></o:p>

e.printStackTrace();<o:p></o:p>

}<o:p></o:p>

<o:p></o:p>

<o:p></o:p>

}<o:p></o:p>

};<o:p></o:p>

//we can use some non-existing path as result file name - all real saving will be done in CustomerHtmlSavingStrategy

String outHtmlFile = “Z:/SomeNonExistingFolder/SomeUnexistingFile.html”;<o:p></o:p>

doc.save(outHtmlFile, newOptions);


Please feel free to contact us for any further assistance.


Best Regards,

Hello Tilal,


Thank you for the quick response.

Actually, I have used this example which I found somewhere in this forum. Unfortunately, without success (may be I missed something?).

I have modified your example a bit and used another type of output stream:
newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy() {
    @Override
public void invoke(HtmlSaveOptions.HtmlPageMarkupSavingInfo htmlSavingInfo) {
byte[] resultHtmlAsBytes = new byte[(int) htmlSavingInfo.ContentStream.getLength()];
htmlSavingInfo.ContentStream.read(resultHtmlAsBytes, 0, resultHtmlAsBytes.length);
// here You can use any writable stream, file stream is taken just as example
	ByteArrayOutputStream dst = new ByteArrayOutputStream();
try {
dst.write(resultHtmlAsBytes);
dst.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
//we can use some non-existing path as result file name - all real saving will be done in CustomerHtmlSavingStrategy
String outHtmlFile = “Z:/SomeNonExistingFolder/SomeUnexistingFile.html”;
document.save(outHtmlFile, newOptions);
The errors which I got:
Exception in thread “main” class com.aspose.pdf.internal.p364.z30: DirectoryInfo was not created
com.aspose.pdf.internal.p364.z4.m1(Unknown Source)
com.aspose.pdf.ADocument.save(Unknown Source)
com.aspose.pdf.Document.save(Unknown Source)
and
at com.aspose.pdf.internal.p364.z4.m1(Unknown Source)
at com.aspose.pdf.ADocument.save(Unknown Source)
at com.aspose.pdf.Document.save(Unknown Source)


Thanks,
Andrei

Hi Andrei,


Thanks for your feedback. I am looking into your issue and will let you know my findings soon.

Best Regards,

Hi Andrei,


Thanks for your patience. I have also noticed the stated exception and logged a ticket PDFNEWJAVA-35046 for further investigation and rectification. We will keep you updated about the issue resolution progress.

We are sorry for the inconvenience caused.

Best Regards,

Hello Tilal,


Is there any updates related to the issue reported above?

Thanks,
Andrei

Hi Andrei,


As we recently have been able to notice this issue, and until or
unless we have investigated and have figured out the actual reasons of this
problem, we might not be able to share any timelines by which this problem will
be resolved.<o:p></o:p>

However, as soon as we have made some significant progress towards the resolution of this issue, we would be more than happy to update you with the status of correction. Please be patient and spare us little time. Your patience and comprehension is greatly appreciated in this regard.

Hi there


I trid to modify the code up there, and I got an error message:

Inconsistent saving options detected : ‘CustomStrategyOfCssUrlCreation’,‘CustomCssSavingStrategy’,‘CustomResourceSavingStrategy’ may not be null when requested saving to stream!

Is there any examples for these 3 callback?

Hi Craig,


Thanks for your inquiry. We will appreciate it if you please share some more details about your issue, do you want to save each resource file in a separate stream?

However if you want to get a single stream with embedded resources then you need to implement only CustomHtmlSavingStrategy callback. Please check following documentation link for details, it will help you to accomplish the task.


Best Regard,