Save CSS and Images when Exporting To HTML

Hi,

In the latest Aspose.Slides for Java (17.9.0, 17.9.1), I’ve noticed you’ve added the feature to save CSS and images into a separate folder when converting a presentation to HTML. Looking at the latest docs, I can’t find the guide how to do this.

Can you tell me how can I specify the folder where images should be saved to?

Thanks,
Zeljko

@Zeljko,

I have observed your comments. Can you please visit this documentation article. This will help you to achieve your requirements. Please share feedback with us if there is still an issue.

Hi Adnan,

In the previous post, I’ve linked the same article you provided me with. I did not manage to find the way to achieve my requirement. To be more specific, it looks like the class CustomHeaderAndFontsController is not present in the latest Aspose.Slides library.

Can you check this?

Thanks,
Zeljko

@Zeljko,

I have observed your requirements. I have shared code snippet in text file with you. Please check attachment. This will help you to achieve your requirements.CSSandImages.zip (1.1 KB)

Hi Adnan,

In the release notes of the Aspose.Slides 17.9.0, you can find this: SLIDESNET-38899 - Option for saving CSS and images separately when exporting as HTML - Feature. It made me think there’s now the way to specify a images folder without using the code snippet you provided me with, as this code snippet has been available for some time.
If this is not the case, what is SLIDESNET-38899 about?

Also, if I use the code snippet you provided (slightly adjusted), I have a problem with missing images when saving each slide as separate HTML file.

@Test
public void testHtmlForEachSlide() {

Presentation pres = new Presentation("/aspose/Test.pptx");

String imagesFolder = "/aspose/html_each_slide/images/";

ExternalResourcesHtmlController htmlController = new ExternalResourcesHtmlController(imagesFolder);

HtmlOptions htmlOptions = new HtmlOptions(htmlController);

htmlOptions.setHtmlFormatter(HtmlFormatter.createCustomFormatter(htmlController));

ISlideCollection slides = pres.getSlides();

for (int i = 1; i < slides.size() + 1; i++) {
    try (OutputStream outputStream = new FileOutputStream(
	    "/aspose/html_each_slide/slide" + i + ".html")) {
	pres.save(outputStream, new int[] { i }, SaveFormat.Html, htmlOptions);
    } catch (Exception e) {
	e.printStackTrace();
    }
}

pres.dispose();

}

@Test
public void testHtmlWholePresentation() {

Presentation pres = new Presentation("/aspose/Test.pptx");

String imagesFolder = "/aspose/html_whole_presentation/images/";

ExternalResourcesHtmlController htmlController = new ExternalResourcesHtmlController(imagesFolder);

HtmlOptions htmlOptions = new HtmlOptions(htmlController);

htmlOptions.setHtmlFormatter(HtmlFormatter.createCustomFormatter(htmlController));

pres.save("/aspose/html_whole_presentation/Test.html", SaveFormat.Html, htmlOptions);

pres.dispose();

}

public class ExternalResourcesHtmlController implements IHtmlFormattingController, ILinkEmbedController {
private String header = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n"
	+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
	+ "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=11\">\n" + "</head>";
private String urlTemplate = "%d.%s";
private String imagesFolder;

private ResponsiveHtmlController controller = new ResponsiveHtmlController();
private Map<Integer, String> resourceUrls = new HashMap<>();

public ExternalResourcesHtmlController(String imagesFolder) {
    this.imagesFolder = imagesFolder;
}

@Override
public int getObjectStoringLocation(int id, byte[] entityData, String semanticName, String contentType,
	String recomendedExtension) {
    switch (contentType) {
    case "image/svg+xml": {
	return LinkEmbedDecision.Embed;
    }
    default: {
	String url = String.format(urlTemplate, id, recomendedExtension);
	resourceUrls.put(id, url);
	return LinkEmbedDecision.Link;
    }
    }
}

@Override
public String getUrl(int id, int referrer) {
    return "images/" + resourceUrls.get(id);
}

@Override
public void saveExternal(int id, byte[] entityData) {
    try {
	FileUtils.writeByteArrayToFile(new File(imagesFolder + resourceUrls.get(id)), entityData);
    } catch (IOException e) {
	e.printStackTrace();
    }
}

@Override
public void writeDocumentEnd(IHtmlGenerator arg0, IPresentation arg1) {
    controller.writeDocumentEnd(arg0, arg1);
}

@Override
public void writeDocumentStart(IHtmlGenerator arg0, IPresentation arg1) {
    arg0.addHtml(header);
}

@Override
public void writeShapeEnd(IHtmlGenerator arg0, IShape arg1) {
    controller.writeShapeEnd(arg0, arg1);
}

@Override
public void writeShapeStart(IHtmlGenerator arg0, IShape arg1) {
    controller.writeShapeStart(arg0, arg1);
}

@Override
public void writeSlideEnd(IHtmlGenerator arg0, ISlide arg1) {
    controller.writeSlideEnd(arg0, arg1);
}

@Override
public void writeSlideStart(IHtmlGenerator arg0, ISlide arg1) {
    controller.writeSlideStart(arg0, arg1);
}
}

I’ve done some debugging and managed to find what’s causing this problem. In both getObjectStoringLocation(int id, byte[] entityData, String semanticName, String contentType, String recomendedExtension) and getUrl(int id, int referrer) methods, sometimes I get the same id for different images. This id has to be unique, am I right?

Please find test files attached 1302.zip (2.5 MB)

Can you check this?

Thanks,
Zeljko

@Zeljko,

I have observed your comments. An issue with ID SLIDESJAVA-36644 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed.

The issues you have found earlier (filed as SLIDESJAVA-36644) have been fixed in Aspose.Slides for Java 17.12. Please try using the latest release version and in case you experience any issue or you have any further query, please feel free to contact.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan