2.
- Code: Select all
public static String convertHtmlToDocToPdf(String htmlString) throws IOException {
Path tempHTMLFile = Files.createTempFile("output", ".html");
Path tempWordFile = Files.createTempFile("HTMLtoWord", ".docx");
File tempPdfFile = File.createTempFile(
"WordToPDF1_" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + "_", ".pdf");
logger.info(htmlString);
try {
FileWriterWithEncoding fw = new FileWriterWithEncoding(tempHTMLFile.toFile().getPath(),
StandardCharsets.UTF_16);
fw.write(htmlString);
fw.close();
logger.info("tempHTMLFile file created: " + tempHTMLFile.toFile().getPath());
// Set license key
com.spire.license.LicenseProvider.setLicenseKey(ConvertConstants.LICENSE_KEY);
com.spire.license.LicenseProvider.loadLicense();
com.spire.doc.license.LicenseProvider.setLicenseKey(ConvertConstants.LICENSE_KEY);
com.spire.doc.license.LicenseProvider.loadLicense();
Document.setGlobalCustomFontsFolders("Fonts");
// Create a new Document object
Document htmlDocument = new Document();
// Load the HTML file "data/sample input 1.html" into the document
htmlDocument.loadFromFile(tempHTMLFile.toFile().getPath().toString(), FileFormat.Html);
// Save the document as a Word document with the name "output/HTMLtoWord.docx"
htmlDocument.saveToFile(tempWordFile.toFile().getPath().toString(), FileFormat.Docx);
Document.setGlobalCustomFontsFolders("Fonts");
// Create a new Document object for reading the saved Word document
Document wordDocument = new Document();
// Load the saved Word document "output/HTMLtoWord.docx" into the document
wordDocument.loadFromFile(tempWordFile.toFile().getPath().toString(), FileFormat.Docx);
logger.info("tempWordFile file created: " + tempWordFile.toFile().getPath());
ToPdfParameterList ppl = new ToPdfParameterList();
ppl.isEmbeddedAllFonts(true);
ppl.setDisableLink(false);
wordDocument.setJPEGQuality(40);
// Save the modified document as a PDF with the name "output/WordToPDF1.pdf"
wordDocument.saveToFile(tempPdfFile.getAbsolutePath(), ppl);
logger.info("tempPdfFile file created: " + tempPdfFile.getAbsolutePath());
} catch (IOException e) {
throw e;
}
return addPageNumberToPDF(tempPdfFile.getAbsolutePath());
}
3. Manifest-Version: 1.0
Extension-Name: spire.doc
Implementation-Title: spire.doc for java
Implementation-Version: 12.2.2
Implementation-Vendor: E-iceblue Co., Ltd.
Implementation-Vendor-Id: com.spire
Implementation-URL: https://www.e-iceblue.com
4. Application Type: Spingboot JDK 1.8