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