An example of implementing setCustomResourceSavingStrategy when converting pdf to html

Hi,

I am trying to convert pdf to html stream and looking for an example for implementing CustomResourceSavingStrategy.

Thanks

@rgutta

A task as PDFJAVA-42146 has already been logged in our issue tracking system to prepare an example for this feature. It is linked with this forum thread so that you will receive a notification as soon as the task is resolved. Please be patient and spare us some time.

@rgutta

Please, use the following code snippet, where you can configure where to save resources even if HTML is saved into a stream:

public static void main(String[] args) {

        initLicense();

        String myDir = "C:/testdata/";

        Document pdf = new Document(myDir + "document.pdf");

        HtmlSaveOptions htmlSaveOps = new HtmlSaveOptions();
        htmlSaveOps.setRasterImagesSavingMode(HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground);
        htmlSaveOps.setFontSavingMode(HtmlSaveOptions.FontSavingModes.AlwaysSaveAsWOFF);
        htmlSaveOps.setLettersPositioningMethod(LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss);
        htmlSaveOps.setSplitIntoPages(false);

//this option should not be used, when we manually configure resource saving
//        htmlSaveOps.setPartsEmbeddingMode(HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml);

        final String dirName = myDir + "resourcesLocal/";
        java.io.File file = new java.io.File("" + dirName);
        file.mkdirs();

        //CustomCssSavingStrategy
        htmlSaveOps.setCustomCssSavingStrategy(new HtmlSaveOptions.CssSavingStrategy()
        {
            @Override
            public void invoke(HtmlSaveOptions.CssSavingInfo partSavingInfo) {
                writeCssToResourceFolder(partSavingInfo, dirName);
            }
        });
        htmlSaveOps.setCustomStrategyOfCssUrlCreation(new HtmlSaveOptions.CssUrlMakingStrategy()
        {
            public String invoke(HtmlSaveOptions.CssUrlRequestInfo requestInfo)
            {
                return strategy_CSS_ReturnResultPathInPredefinedFolder(requestInfo, dirName);
            }
        });
        htmlSaveOps.setCustomResourceSavingStrategy(new HtmlSaveOptions.ResourceSavingStrategy()
        {
            public String invoke(SaveOptions.ResourceSavingInfo resourceInfo)
            {
                return strategyResourcesToFiles(resourceInfo, dirName);
            }
        });

        for (int p = 1; p <= 1; p++) {
            Document pageDoc = new Document();
            pageDoc.getPages().add(pdf.getPages().get_Item(p));

            String outHtmlFile = myDir + "_FontSubst_SomeUnexistingFile" + p + ".html";
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            pageDoc.save(output, htmlSaveOps);

//for testing purposes we can save the stream to disk or any other location to open it and check result in a browser
            try (FileOutputStream outputStream = new FileOutputStream(outHtmlFile)) {
                outputStream.write(output.toByteArray());
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    private String strategyResourcesToFiles(HtmlSaveOptions.ResourceSavingInfo resourceInfo, String myDir)
    {
        //handle resources:  NodeLevelResourceType.Image and NodeLevelResourceType.Font

        String URL = null;
        if (resourceInfo.getResourceType() == HtmlSaveOptions.NodeLevelResourceType.Image) {

            // cast resource info to ImageSavingInfo cause ImageSavingInfo contains
            // page numbers
            HtmlSaveOptions.HtmlImageSavingInfo asImageSavingInfo = (HtmlSaveOptions.HtmlImageSavingInfo) resourceInfo;
            // get prefix - something like 'page_1_2_'
            String prefix = "page_" + asImageSavingInfo.getPdfHostPageNumber() + "_" + asImageSavingInfo.getHtmlHostPageNumber() + "_";
            // get target file name and write content to it
            URL = myDir + prefix + resourceInfo.getSupposedFileName();

        }else  if (resourceInfo.getResourceType() == HtmlSaveOptions.NodeLevelResourceType.Font) {

            // get target file name and write content to it
            URL = myDir + resourceInfo.getSupposedFileName();

        }

        try (FileOutputStream outputStream = new FileOutputStream(URL)) {
            outputStream.write(resourceInfo.getContentStream());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return URL;
    }
    private String strategy_CSS_ReturnResultPathInPredefinedFolder(HtmlSaveOptions.CssUrlRequestInfo requestInfo, String myDir)
    {
        return "file:///" + myDir.replace("\\", "/") + "css_files/" + "css_style.css";
    }

    private void writeCssToResourceFolder(HtmlSaveOptions.CssSavingInfo resourceInfo, String myDir)
    {
        String css_files_dir = myDir + "css_files/";
        java.io.File theDir = new java.io.File(css_files_dir);
        if (!theDir.exists()){
            theDir.mkdirs();
        }

        String fullPathWithGuid = resourceInfo.getSupposedURL().substring(8/*cut the length of the prefix "file:///" */);

        try (FileOutputStream outputStream = new FileOutputStream(fullPathWithGuid)) {
            InputStream steamCss = resourceInfo.getContentStream();
            byte[] bytes = new byte[steamCss.available()];
            DataInputStream dis = new DataInputStream(steamCss);
            dis.readFully(bytes);
            outputStream.write(bytes);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }