PDF to embedded HTML - save options validation

I convert PDF to embedded HTML (i.e. all images, CSS in one file), the following works fine:

HtmlSaveOptions saveOpts = new HtmlSaveOptions();
saveOpts.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;
saveOpts.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
saveOpts.LettersPositioningMethod = LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;
pdf.save(filepath, saveOpts);

Then I change filepath to output stream, it fails:
Inconsistent saving options detected : ‘CustomStrategyOfCssUrlCreation’,‘CustomCssSavingStrategy’,‘CustomResourceSavingStrategy’ may not be null when requested saving to stream!

Following the advice, I set some dummy implementation (assuming they will not be called) to these strategies.
saveOpts.CustomCssSavingStrategy = new CssSavingStrategy() {
@Override
public void invoke(CssSavingInfo arg0) {
throw new UnsupportedOperationException();
}
};
saveOpts.CustomStrategyOfCssUrlCreation = new CssUrlMakingStrategy() {
@Override
public String invoke(CssUrlRequestInfo arg0) {
throw new UnsupportedOperationException();
}
};
saveOpts.CustomResourceSavingStrategy = new ResourceSavingStrategy() {
@Override
public String invoke(ResourceSavingInfo arg0) {
throw new UnsupportedOperationException();
}
};

It still fails:
If selected mode of embedding into HTML(PartsEmbeddingModes.EmbedAllIntoHtml), custom CSS saving stategy not allowed and must be null!

The save options validation is self contradicting.

Hi Samuel,

Thanks for your inquiry.

In order to save output to MemoryStream, you do not need to set valueof saveOpts.PartsEmbeddingMode in your code and make sure to return a valid directory in custom methods for CSSUrlCreation and ResourceSavingStrategy. Please check following code snippet where I have successfully implemented the strategies to save the output into OutputStream object.

public void PDFtoHtml()
{
    Document doc = new Document(dataDir + “pdf - sample - copy.pdf”);
    HtmlSaveOptions saveOpts = new HtmlSaveOptions();
    //saveOpts.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;
    saveOpts.LettersPositioningMethod = LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;
    saveOpts.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
    saveOpts.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;
    saveOpts.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy() {
@Override
public void invoke(CssSavingInfo arg0)
    {
        // TODO Auto-generated method stub
    }
};

saveOpts.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy() {

@Override
public String invoke(CssUrlRequestInfo arg0)
{
    // TODO Auto-generated method stub
    return { Some Valid Directory};
}
};

saveOpts.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy() {

@Override
public String invoke(ResourceSavingInfo arg0)
{
    // TODO Auto-generated method stub
    return { Some Valid Directory};
}
};

ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
System.out .println(“Now saving…”);
doc.save(dstStream, saveOpts);
}

Please try using above code snippet to save the result in output stream and in case you still face any issue, please share your sample input document along with information regarding your environment and API version, so that we can test the scenario in our environment and address it accordingly.

Best Regards,

Hi Asad,

Thanks for your help, but that does not answer my question. I want everything in a single HTML file, so PartsEmbeddingModes.EmbedAllIntoHtml should be on. Also I want to output the HTML to Servlet response output-stream. The save options validation does not let me do these 2 things together.

Regards,
Samuel

Hi Samuel,


Thanks for writing back. I have tried to save the PDF into single HTML file physically with all embedded resources and I got success. Furthermore, I am checking details with more complex documents (i.e with images and fonts) for saving them into OutputStream after conversion, and will update you about results in a while. Please be patient.


Best Regards,

Hi Samuel,


Thanks for your patience.

We have tested the scenario in our environment and observed that API is generating issue while saving output HTML into stream object, when PartsEmbeddingMode is set to EmbedAllIntoHtml. We have raised an investigation ticket as PDFJAVA-36745 in our issue tracking system, for the sake of detailed investigation. We will certainly keep you informed as soon as we have some definite updates from product team in this regard.

Please be patient and spare us little time. We are sorry for the inconvenience.


Best Regards,