Java Code Examples for org.docx4j.Docx4J#toPDF()

The following examples show how to use org.docx4j.Docx4J#toPDF() . 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: HtmlConverter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * 将 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 存为 pdf
 *
 * @param wordMLPackage
 * @return
 * @throws Exception
 */
public File savePdf(WordprocessingMLPackage wordMLPackage) throws Exception {

    File file = new File(genFilePath() + ".pdf");

    OutputStream os = new java.io.FileOutputStream(file);

    Docx4J.toPDF(wordMLPackage, os);

    os.flush();
    os.close();

    if (logger.isDebugEnabled()) {
        logger.debug("Save to [.pdf]: {}", file.getAbsolutePath());
    }
    return file;
}
 
Example 2
Source File: DocxProducer.java    From OfficeProducer with Apache License 2.0 6 votes vote down vote up
/**
 * docx文档转换为PDF
 *
 * @param wordMLPackage
 * @param pdfPath       PDF文档存储路径
 * @throws Exception
 */
public static void convertDocxToPDF(WordprocessingMLPackage wordMLPackage,
                                    String pdfPath)
        throws Exception {
    //HashSet<String> features = new HashSet<>();
    //features.add(PP_PDF_APACHEFOP_DISABLE_PAGEBREAK_LIST_ITEM);
    //WordprocessingMLPackage process = Preprocess.process(wordMLPackage, features);

    FileOutputStream fileOutputStream = new FileOutputStream(pdfPath);
    Docx4J.toPDF(wordMLPackage, fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();

    /*FOSettings foSettings = Docx4J.createFOSettings();
    foSettings.setWmlPackage(wordMLPackage);
    Docx4J.toFO(foSettings, fileOutputStream, Docx4J.FLAG_EXPORT_PREFER_XSL);*/
}
 
Example 3
Source File: WordprocessingMLPackageWriter.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * 将 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 存为 pdf
 * @param wmlPackage {@link WordprocessingMLPackage} 对象
 * @param output 文件输出流
 * @throws IOException :IO异常
 * @throws Docx4JException : Docx4j异常
 */
public void writeToPDF(WordprocessingMLPackage wmlPackage,OutputStream output) throws IOException, Docx4JException {
	Assert.notNull(wmlPackage, " wmlPackage is not specified!");
	Assert.notNull(output, " output is not specified!");
       try {
		Docx4J.toPDF(wmlPackage, output); //保存到 pdf 文件
		output.flush();
	} finally{
		IOUtils.closeQuietly(output);
       }
}
 
Example 4
Source File: TextBoxTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
private static void execute(WordprocessingMLPackage wordMLPackage) throws Docx4JException, IOException {
	
   	// Pretty print the main document part
	System.out.println(
			XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true) );
	
	// Optionally save it
	if (save) {
		String filename = System.getProperty("user.dir") + "/OUT_CreateWordprocessingMLDocument.docx";
		wordMLPackage.save(new File(filename) );
		System.out.println("Saved " + filename);
	}
	
	// FO
	if (fo) {
		
		OutputStream baos = new ByteArrayOutputStream();
		Exporter<FOSettings> exporter = FOExporterVisitor.getInstance();
		FOSettings settings = new FOSettings();
		settings.setWmlPackage(wordMLPackage);
		settings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
		exporter.export(settings, baos);
		
		System.out.println( ((ByteArrayOutputStream) baos).toString("UTF-8"));
		
	} else {
		
		Docx4J.toPDF(wordMLPackage, 
				FileUtils.openOutputStream(new File(System.getProperty("user.dir") + "/OUT_textbox.pdf")));
		
	}		
}
 
Example 5
Source File: DocToPDFConverter.java    From docs-to-pdf-converter with MIT License 4 votes vote down vote up
@Override
public void convert() throws Exception{

	loading();

	InputStream iStream = inStream;


	WordprocessingMLPackage wordMLPackage = getMLPackage(iStream);


	processing();
	Docx4J.toPDF(wordMLPackage, outStream);

	finished();
	
}
 
Example 6
Source File: Conversion.java    From docx4j-export-FO with Apache License 2.0 2 votes vote down vote up
/** Create a pdf version of the document, using XSL FO. 
 * 
 * @param os
 *            The OutputStream to write the pdf to 
 * @param settings
 *            The configuration for the conversion 
 * 
 * */     
public void output(OutputStream os, PdfSettings settings) throws Docx4JException {
	setupSettings(settings, FOSettings.INTERNAL_FO_MIME);
	Docx4J.toPDF(wordMLPackage, os);
}