Page 1 of 1

PDF generation failed after Inserting HTML in a table cell

PostPosted: Thu Nov 26, 2015 9:03 pm
by codeLover
Hi ,

I attempt to insert HTML data into Rich Text Content Control into a table cell in my template (.docx).

Extract code :
Code: Select all
    AlternativeFormatInputPart afiPart = null;
    afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); //CAUTION: each html part needs a new name!!

    afiPart.setBinaryData(html.getBytes("UTF-8"));
   afiPart.setContentType(new ContentType("text/html"));

    Relationship altChunkRel = null;

    altChunkRel = template.getMainDocumentPart().addTargetPart(afiPart);
                   
    CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
    ac.setId(altChunkRel.getId());   
                   
    Tc parent = (Tc)toReplace.getParent();           
    int subIndex = parent.getContent().indexOf(p);
    parent.getContent().set(subIndex,ac);

    OutputStream os = new java.io.FileOutputStream(outputPdfPath + ".pdf");
    Docx4J.toPDF(wordMLPackage, os);
    os.close();


The output is all right in .docx however it's failed in PDF generation with error below :

Exception executing transformer: org.apache.fop.fo.ValidationException: "fo:table-cell" is missing child elements. Required content model: marker* (%block;)+ (See position 111:608)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:215)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:158)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:139)


When I open the test.zip , there is a hw.html file .In the document.xml , the particular table cell is empty.
Code: Select all
                     <w:t>AA Category</w:t>
                  </w:r>
               </w:p>
            </w:tc>
            <w:tc>
               <w:tcPr>
                  <w:tcW w:w="283" w:type="dxa"/>
               </w:tcPr>
               <w:p>
                  <w:pPr>
                     <w:jc w:val="center"/>
                     <w:rPr>
                        <w:b/>
                        <w:bCs/>
                        <w:sz w:val="16"/>
                        <w:szCs w:val="16"/>
                     </w:rPr>
                  </w:pPr>
                  <w:r>
                     <w:rPr>
                        <w:b/>
                        <w:bCs/>
                        <w:sz w:val="16"/>
                        <w:szCs w:val="16"/>
                     </w:rPr>
                     <w:t>:</w:t>
                  </w:r>
               </w:p>
            </w:tc>
            <w:tc>
               <w:tcPr>
                  <w:tcW w:w="2268" w:type="dxa"/>
                  <w:vAlign w:val="center"/>
               </w:tcPr>
               <w:altChunk r:id="rId13"/>
            </w:tc>
            <w:tc>
               <w:tcPr>
                  <w:tcW w:w="1985" w:type="dxa"/>
               </w:tcPr>
               <w:p>
                  <w:pPr>
                     <w:rPr>
                        <w:b/>
                        <w:bCs/>
                        <w:sz w:val="16"/>
                        <w:szCs w:val="16"/>
                     </w:rPr>
                  </w:pPr>
                  <w:r>
                     <w:rPr>
                        <w:b/>
                        <w:bCs/>
                        <w:sz w:val="16"/>
                        <w:szCs w:val="16"/>
                     </w:rPr>
                     <w:t>Template Lending</w:t>


Isn't that the reason why pdf generation failed?
Any way to overcome this?
Or, I should uses other approach to replace HTML value in table cell.

Appreciate your input.

Thank you very much.

Re: PDF generation failed after Inserting HTML in a table ce

PostPosted: Thu Nov 26, 2015 10:33 pm
by jason
On your MainDocumentPart, invoke:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        /**
         * To convert an altChunk of type XHTML, this method requires docx4j-XHTMLImport.jar (LGPL) and its dependencies.
         * */

        @Override
        public WordprocessingMLPackage convertAltChunks() throws Docx4JException
 
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Note that it'll need to be well formed XML.

Re: PDF generation failed after Inserting HTML in a table ce

PostPosted: Fri Nov 27, 2015 1:16 am
by codeLover
Hi Jason,

Thanks. I tried the below but I still get same error. Any idea why?

Or, how can I verify if the convertAltChunks() worked?

Code: Select all
    wordMLPackage = documentPart.convertAltChunks();

   OutputStream os = new java.io.FileOutputStream(outputPdfPath + ".pdf");
   Docx4J.toPDF(wordMLPackage, os);
   //--- To save as PDF ---END

Re: PDF generation failed after Inserting HTML in a table ce

PostPosted: Fri Nov 27, 2015 9:37 am
by jason
1. type needs to be AltChunkType.Xhtml, not AltChunkType.Html

2. do you have docx4j-XHTMLImport.jar (LGPL) and its dependencies?

Re: PDF generation failed after Inserting HTML in a table ce

PostPosted: Fri Nov 27, 2015 12:57 pm
by codeLover
yes ,docx4j-ImportXHTML-3.2.1.jar .

Em , you meant here I should uses xhtml instead of HTML ?

Code: Select all
    AlternativeFormatInputPart afiPart = null;
    afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); //CAUTION: each html part needs a new name!!

    afiPart.setBinaryData(html.getBytes("UTF-8"));
   afiPart.setContentType(new ContentType("text/html"));


Thanks.

Re: PDF generation failed after Inserting HTML in a table ce

PostPosted: Sat Nov 28, 2015 2:54 am
by codeLover
I tried :

Code: Select all
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();       
        mdp.addParagraphOfText("Paragraph 1");

        // Add the XHTML altChunk
        String xhtml = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>";
        mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes());
       
        mdp.addParagraphOfText("Paragraph 3");
       
        // Round trip
        WordprocessingMLPackage pkgOut = mdp.convertAltChunks();
       
        System.out.println(
                        XmlUtils.marshaltoString(pkgOut.getMainDocumentPart().getJaxbElement(), true, true));
      
        String myTempFolder = "E://Workspace_docx4j/template/" ;       
      String outputPdfPath = myTempFolder + "QuickTest";
      
       OutputStream os = new java.io.FileOutputStream(outputPdfPath + ".docx");
       wordMLPackage.save(os);

       OutputStream os1 = new java.io.FileOutputStream(outputPdfPath + ".pdf");
       Docx4J.toPDF(wordMLPackage, os1);



"Hello World!' is only appear in .docx but not PDF.... :?

Re: PDF generation failed after Inserting HTML in a table ce

PostPosted: Wed Dec 02, 2015 10:32 pm
by jason
Instead of

Code: Select all
wordMLPackage.save(os);

       OutputStream os1 = new java.io.FileOutputStream(outputPdfPath + ".pdf");
       Docx4J.toPDF(wordMLPackage, os1);


try:

Code: Select all
pkgOut .save(os);

       OutputStream os1 = new java.io.FileOutputStream(outputPdfPath + ".pdf");
       Docx4J.toPDF(pkgOut , os1);