org.docx4j.convert.out.HTMLSettings Java Examples

The following examples show how to use org.docx4j.convert.out.HTMLSettings. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WordToHtmlRenditionProvider.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream convert(InputStream fromInputSource, String toMimeType) {
	try {
		WordprocessingMLPackage pkg = WordprocessingMLPackage.load(fromInputSource);

		// Refresh the values of DOCPROPERTY fields
		FieldUpdater updater = new FieldUpdater(pkg);
		updater.update(true);

		AbstractHtmlExporter exporter = new HtmlExporterNG2();
		HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
		htmlSettings.setImageDirPath("/tmp/sample-docx.html_files");
		htmlSettings.setImageTargetUri("/tmp/_files");
		htmlSettings.setWmlPackage(pkg);

		Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);

		OutputStream os = new FileOutputStream("/tmp/temp.html");
		Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
		IOUtils.closeQuietly(os);

		if (pkg.getMainDocumentPart().getFontTablePart() != null) {
			pkg.getMainDocumentPart().getFontTablePart()
					.deleteEmbeddedFontTempFiles();
		}
		// This would also do it, via finalize() methods
		htmlSettings = null;
		pkg = null;

		return new FileInputStream("/tmp/temp.html");
	}
	catch (Exception e) {

	}

	return null;
}
 
Example #2
Source File: Editor.java    From docx-html-editor with GNU Affero General Public License v3.0 4 votes vote down vote up
protected ResponseBuilder streamDocxAsHtml(final WordprocessingMLPackage wordMLPackage, final HttpSession session, 
			EditorHTML editorHTML, HttpServletResponse response) {
		

		// .. the HtmlSettings object
    	final HTMLSettings htmlSettings = new HTMLSettings();    	
		
    	if (editorHTML.equals(EditorHTML.BARE)) {
	    	htmlSettings.setCustomXsltTemplates(BARE_XSLT);
    	} else if  (editorHTML.equals(EditorHTML.CKEditor3)) {
	    	htmlSettings.setCustomXsltTemplates(CKEditor3_XSLT);    		
    	}

    	
    	htmlSettings.setImageHandler(new SessionImageHandler(session));

    	htmlSettings.setStyleElementHandler(new SessionStyleHandler(session));
    	
//    	htmlSettings.setUserBodyTop("<H1>TOP!</H1>");
//    	htmlSettings.setUserBodyTail("<H1>TAIL!</H1>");
		
		// Sample sdt tag handler (tag handlers insert specific
		// html depending on the contents of an sdt's tag).
		// This will only have an effect if the sdt tag contains
		// the string @class=XXX
//			SdtWriter.registerTagHandler("@class", new TagClass() );
		
//		SdtWriter.registerTagHandler(Containerization.TAG_BORDERS, new TagSingleBox() );
//		SdtWriter.registerTagHandler(Containerization.TAG_SHADING, new TagSingleBox() );

		
    	htmlSettings.setWmlPackage(wordMLPackage);
    	wordMLPackage.setUserData("HttpSession", session);
    	
    	// Since we'll store various stuff on the pkg object (eg images, long tail)
    	// avoid using a clone!
    	htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_DEEP_COPY);

    	// Don't convert w:r/w:br[w:@type="page"] to <w:pageBreakBefore/>
    	// since it seems that should really go on the following paragraph
    	htmlSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_MOVE_PAGEBREAK);
    	
		// We'll store the images associated with this docx in a map:
		HashMap<String, byte[]> imageMap = new HashMap<String, byte[]>();  
		// and store that in the docx 
		// (so each docx has its own collection of available images; these aren't shared across
		//   the user's session, if they have multiple windows open, each with a different editor)
		wordMLPackage.setUserData(APP_KEY + "imageMap", imageMap);
		
		htmlSettings.getSettings().put("ContextPath", getContextPath());
		
		// For extension function
		htmlSettings.getSettings().put("HttpServletResponse", response);
    	
    	
		ResponseBuilder builder = Response.ok(
				
				new StreamingOutput() {				
					public void write(OutputStream output) throws IOException, WebApplicationException {					
				         try {
				     		javax.xml.transform.stream.StreamResult result 
				     			= new javax.xml.transform.stream.StreamResult(output);
				    		
				    		// Docx4J.toHTML(htmlSettings, output, Docx4J.FLAG_NONE);
				    		Exporter<HTMLSettings> exporter = new HTMLExporterXslt(CUSTOM_HTML_WRITER_REGISTRY);
				    		exporter.export(htmlSettings, output);
				    		
						} catch (Exception e) {
							throw new WebApplicationException(e);
						}							
					}
				}
			);
//						builder.header("Content-Disposition", "attachment; filename=output.pdf");
//			builder.type("application/pdf");
			
			return builder;		
	}