Sunith M S
Sunith M S

Reputation: 1

Issues with Inline CSS Converting HTML to PowerPoint using XHTMLtoPPTX

I am trying to convert HTML content into PowerPoint slides using the XHTMLtoPPTX library, but I am encountering this issue:

Inline CSS Styles Not Applying Correctly: Some CSS styles, particularly inline styles (like background-color, font-size, font-weight, and text color), are not being applied as expected in the PowerPoint slides. It seems like the styles are not being parsed or transferred correctly during conversion.

String text = "<html><body><p><i style="background-color:rgb(180, 183, 13);">Once</i> <strong>we</strong> <u style="background-color:rgb(183, 13, 175);">have</u> <strong>settled</strong> on the right candidate</p><table style="width: 100%; border: 1px solid black; border-collapse: collapse; background-color:rgb(160, 4, 4);"><tbody><tr style="background-color:rgb(58, 183, 13);"><td>ds</td><td>s</td><td>sf</td></tr><tr><td>f</td><td>s</td><td>ty</td></tr></tbody></table></body></html>";


private void createPPT(String text) throws Exception {
    // Setup target pptx
    PresentationMLPackage presentationMLPackage = getPkg();
    SlidePart slidePart = (SlidePart) presentationMLPackage.getParts().get(new    PartName("/ppt/slides/slide1.xml"));

    StringBuilder content = new StringBuilder();
    content.append("<html><body>");
    content.append(text);
    content.append("</body></html>");

    // Convert HTML to PowerPoint
    XHTMLtoPPTX converter = new XHTMLtoPPTX(presentationMLPackage, slidePart, content.toString(), "");
    List<Object> results = converter.convertSingleSlide();    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().addAll(results);

    // Save the presentation
    String outputFilePath = "/Users/sunith/Documents/new_presentation2.pptx";
    presentationMLPackage.save(new java.io.File(outputFilePath));

    System.out.println("Done! Saved to " + outputFilePath);
}

public static PresentationMLPackage getPkg() throws Exception {
    // Create package and add slide
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
    MainPresentationPart pp = (MainPresentationPart) presentationMLPackage.getParts().getParts().get(new PartName("/ppt/presentation.xml"));
    SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage.getParts().getParts().get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));

    // Create a new slide and add layout
    SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide1.xml"));
    slidePart.setContents(SlidePart.createSld());
    pp.addSlide(0, slidePart);
    slidePart.addTargetPart(layoutPart);

    return presentationMLPackage;
}

Is it possible to convert HTML to PowerPoint in this way, or are there other approaches that might work better?

Upvotes: 0

Views: 78

Answers (1)

JasonPlutext
JasonPlutext

Reputation: 15863

https://github.com/plutext/docx4j-ImportXHTML/blob/VERSION_11_4_10/docx4j-ImportXHTML-core/src/main/java/org/pptx4j/convert/in/xhtml/XHTMLtoPPTX.java#L494 invokes PropertyFactory.createPropertyFromCssName which is at https://github.com/plutext/docx4j/blob/VERSION_11_5_2/docx4j-core/src/main/java/org/docx4j/model/properties/PropertyFactory.java#L543

You can see there the CSS properties which are currently supported.

These do or should include background-color, font-weight, color (for text color) and font-size (depending on the units used).

You can inspect the code there to see whether it should process the XHTML you are feeding in.

Upvotes: 0

Related Questions