義字體為48,打印出來(lái)為36
在切圖網(wǎng)( www.qietu.com )近期的一個(gè)web前端外包H5項(xiàng)目中遇到的問(wèn)題,該項(xiàng)目采用vue+vue-router+jquery+html5+css3等,做出的效果在chrome或者微信上效果非常好,但是放到華為的自帶瀏覽器下就出現(xiàn)了問(wèn)題,經(jīng)過(guò)排查得以解決,做個(gè)筆記。
1,vue2.x版本最好不要采用最新JS版本 es5或者es6寫(xiě)法,比如let 都會(huì)導(dǎo)致兼容性問(wèn)題
2,華為自帶瀏覽器本身的bug,設(shè)置html根字體大小為 48px時(shí)候,依然會(huì)變成 36px,以下是網(wǎng)上其他人遇到的問(wèn)題:
在華為內(nèi)置的瀏覽器中,我 document.documentElement.style.fontSize設(shè)置字體大小為48px時(shí),不起作用,瀏覽器還是按照其它的字體大小(36px)來(lái)進(jìn)行布局,窗口的寬度為360;
和我們遇到的問(wèn)題一致,從而得出華為瀏覽器本身的問(wèn)題
3,華為瀏覽器采用uc的精簡(jiǎn)版內(nèi)核,也會(huì)出bug,所以有時(shí)候網(wǎng)頁(yè)出問(wèn)題,實(shí)在解決不了的時(shí)候,也要考慮是不是瀏覽器本身的問(wèn)題。
好了,先寫(xiě)到這里。
切圖網(wǎng)專(zhuān)注psd轉(zhuǎn)html/sketch轉(zhuǎn)html等web前端外包服務(wù)以及web前端培訓(xùn)。
——PSD轉(zhuǎn)HTML
在將高像素PSD文件轉(zhuǎn)換成 HTML的過(guò)程中,總有不少人會(huì)糾結(jié)是選擇自動(dòng)編碼還是手動(dòng)編碼?二者區(qū)別在哪兒呢?許多人認(rèn)為先進(jìn)技術(shù)在當(dāng)今世界扮演著重要角色,人類(lèi)可以利用高科技做很多事情,完全沒(méi)有必要親自動(dòng)手了,但同時(shí),一些網(wǎng)站開(kāi)發(fā)界的大神如是說(shuō):使用技術(shù)只是為了簡(jiǎn)化程序,離開(kāi)人,機(jī)器將變得一文不值。
PSD轉(zhuǎn)HTML
關(guān)于手動(dòng)編碼和自動(dòng)編碼的區(qū)別,這里做個(gè)全方位比較,供大家參考:
自動(dòng)編碼在將PSD文件轉(zhuǎn)換成HTML語(yǔ)言時(shí),缺乏審美價(jià)值和準(zhǔn)確性。所以,通常情況下,開(kāi)發(fā)人員還是傾向于手動(dòng)將PSD文件轉(zhuǎn)換成完美的html代碼語(yǔ)言,手動(dòng)編碼更能保證SEO語(yǔ)義的可靠性,并且兼容所有瀏覽器。
企業(yè)希望網(wǎng)絡(luò)開(kāi)發(fā)者能盡可能為用戶提供最好的網(wǎng)站,因此,他很可能會(huì)選擇一名精通手工編碼的開(kāi)發(fā)者,以保證在自動(dòng)編碼不匹配時(shí),能夠?qū)SD通過(guò)手工編碼轉(zhuǎn)換成高質(zhì)量的HTML語(yǔ)言。
當(dāng)企業(yè)決定為本公司創(chuàng)建一個(gè)官網(wǎng),在雇用一名開(kāi)發(fā)人員時(shí),他首先關(guān)心的便是預(yù)算,在常規(guī)網(wǎng)站工作中,相對(duì)于手動(dòng)編碼,選擇自動(dòng)編碼會(huì)劃算的多,能為公司減少成本,但是,涉及到重大項(xiàng)目,企業(yè)還是會(huì)優(yōu)先考慮精通手動(dòng)編碼者。
對(duì)于開(kāi)發(fā)人員來(lái)說(shuō),將單獨(dú)網(wǎng)站的PSD文件盡可能快的轉(zhuǎn)換成html代碼是至關(guān)重要的,在這種情況下,自動(dòng)編碼的方法是可取的,因?yàn)樗鄬?duì)于而言,速度快,節(jié)省時(shí)間。
在分辨率極高的PSD文件轉(zhuǎn)換過(guò)程中,要求編寫(xiě)整潔、誤差小、表格少、代碼易讀的頁(yè)面,然而,大多數(shù)自動(dòng)化編碼軟件是完不成的,必須找專(zhuān)業(yè)的開(kāi)發(fā)人員來(lái)做。
經(jīng)過(guò)以上5個(gè)方面的比較,可以看出,手動(dòng)編碼和自動(dòng)編碼各有優(yōu)勢(shì),如果編程基礎(chǔ)薄弱,可以考慮選擇自動(dòng)編碼方式;編程能力較強(qiáng),同時(shí)對(duì)編碼質(zhì)量要求比較高,那就選擇手動(dòng)編碼方式,當(dāng)然這也取決于公司的業(yè)務(wù)需求、預(yù)算、預(yù)期質(zhì)量和個(gè)性化需求等等。
實(shí)現(xiàn)文檔在線預(yù)覽的方式除了上篇文章 文檔在線預(yù)覽新版(一)通過(guò)將文件轉(zhuǎn)成圖片實(shí)現(xiàn)在線預(yù)覽功能說(shuō)的將文檔轉(zhuǎn)成圖片的實(shí)現(xiàn)方式外,還有轉(zhuǎn)成pdf,前端通過(guò)pdf.js、pdfobject.js等插件來(lái)實(shí)現(xiàn)在線預(yù)覽,以及本文將要說(shuō)到的將文檔轉(zhuǎn)成html的方式來(lái)實(shí)現(xiàn)在線預(yù)覽。
以下代碼分別提供基于aspose、pdfbox、spire來(lái)實(shí)現(xiàn)來(lái)實(shí)現(xiàn)txt、word、pdf、ppt、word等文件轉(zhuǎn)圖片的需求。
Aspose 是一家致力于.Net ,Java,SharePoint,JasperReports和SSRS組件的提供商,數(shù)十個(gè)國(guó)家的數(shù)千機(jī)構(gòu)都有用過(guò)aspose組件,創(chuàng)建、編輯、轉(zhuǎn)換或渲染 Office、OpenOffice、PDF、圖像、ZIP、CAD、XPS、EPS、PSD 和更多文件格式。注意aspose是商用組件,未經(jīng)授權(quán)導(dǎo)出文件里面都是是水印(尊重版權(quán),遠(yuǎn)離破解版)。
需要在項(xiàng)目的pom文件里添加如下依賴(lài)
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>23.1</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>23.1</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>23.1</version>
</dependency>
因?yàn)閍spose和spire雖然好用,但是都是是商用組件,所以這里也提供使用開(kāi)源庫(kù)操作的方式的方式。
POI是Apache軟件基金會(huì)用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能。
Apache PDFBox是一個(gè)開(kāi)源Java庫(kù),支持PDF文檔的開(kāi)發(fā)和轉(zhuǎn)換。 使用此庫(kù),您可以開(kāi)發(fā)用于創(chuàng)建,轉(zhuǎn)換和操作PDF文檔的Java程序。
需要在項(xiàng)目的pom文件里添加如下依賴(lài)
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>5.2.0</version>
</dependency>
spire一款專(zhuān)業(yè)的Office編程組件,涵蓋了對(duì)Word、Excel、PPT、PDF等文件的讀寫(xiě)、編輯、查看功能。spire提供免費(fèi)版本,但是存在只能導(dǎo)出前3頁(yè)以及只能導(dǎo)出前500行的限制,只要達(dá)到其一就會(huì)觸發(fā)限制。需要超出前3頁(yè)以及只能導(dǎo)出前500行的限制的這需要購(gòu)買(mǎi)付費(fèi)版(尊重版權(quán),遠(yuǎn)離破解版)。這里使用免費(fèi)版進(jìn)行演示。
spire在添加pom之前還得先添加maven倉(cāng)庫(kù)來(lái)源
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
接著在項(xiàng)目的pom文件里添加如下依賴(lài)
免費(fèi)版:
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.office.free</artifactId>
<version>5.3.1</version>
</dependency>
付費(fèi)版版:
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.office</artifactId>
<version>5.3.1</version>
</dependency>
public static String wordToHtmlStr(String wordPath) {
try {
Document doc = new Document(wordPath); // Address是將要被轉(zhuǎn)化的word文檔
String htmlStr = doc.toString();
return htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
驗(yàn)證結(jié)果:
public String wordToHtmlStr(String wordPath) throws TransformerException, IOException, ParserConfigurationException {
String htmlStr = null;
String ext = wordPath.substring(wordPath.lastIndexOf("."));
if (ext.equals(".docx")) {
htmlStr = word2007ToHtmlStr(wordPath);
} else if (ext.equals(".doc")){
htmlStr = word2003ToHtmlStr(wordPath);
} else {
throw new RuntimeException("文件格式不正確");
}
return htmlStr;
}
public String word2007ToHtmlStr(String wordPath) throws IOException {
// 使用內(nèi)存輸出流
try(ByteArrayOutputStream out = new ByteArrayOutputStream()){
word2007ToHtmlOutputStream(wordPath, out);
return out.toString();
}
}
private void word2007ToHtmlOutputStream(String wordPath,OutputStream out) throws IOException {
ZipSecureFile.setMinInflateRatio(-1.0d);
InputStream in = Files.newInputStream(Paths.get(wordPath));
XWPFDocument document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create().setIgnoreStylesIfUnused(false).setImageManager(new Base64EmbedImgManager());
// 使用內(nèi)存輸出流
XHTMLConverter.getInstance().convert(document, out, options);
}
private String word2003ToHtmlStr(String wordPath) throws TransformerException, IOException, ParserConfigurationException {
org.w3c.dom.Document htmlDocument = word2003ToHtmlDocument(wordPath);
// Transform document to string
StringWriter writer = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(htmlDocument), new StreamResult(writer));
return writer.toString();
}
private org.w3c.dom.Document word2003ToHtmlDocument(String wordPath) throws IOException, ParserConfigurationException {
InputStream input = Files.newInputStream(Paths.get(wordPath));
HWPFDocument wordDocument = new HWPFDocument(input);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {
System.out.println(pictureType);
if (PictureType.UNKNOWN.equals(pictureType)) {
return null;
}
BufferedImage bufferedImage = ImgUtil.toImage(content);
String base64Img = ImgUtil.toBase64(bufferedImage, pictureType.getExtension());
// 帶圖片的word,則將圖片轉(zhuǎn)為base64編碼,保存在一個(gè)頁(yè)面中
StringBuilder sb = (new StringBuilder(base64Img.length() + "data:;base64,".length()).append("data:;base64,").append(base64Img));
return sb.toString();
});
// 解析word文檔
wordToHtmlConverter.processDocument(wordDocument);
return wordToHtmlConverter.getDocument();
}
public String wordToHtmlStr(String wordPath) throws IOException {
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Document document = new Document();
document.loadFromFile(wordPath);
document.saveToFile(outputStream, FileFormat.Html);
return outputStream.toString();
}
}
public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new StringWriter();
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
return writer.toString();
}
驗(yàn)證結(jié)果:
public String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new StringWriter();
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
return writer.toString();
}
public String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(pdfPath);
return outputStream.toString();
}
}
public static String excelToHtmlStr(String excelPath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(excelPath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
DataFormatter dataFormatter = new DataFormatter();
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
StringBuilder htmlStringBuilder = new StringBuilder();
htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");
htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");
htmlStringBuilder.append("</head><body><table>");
for (Row row : sheet) {
htmlStringBuilder.append("<tr>");
for (Cell cell : row) {
CellType cellType = cell.getCellType();
if (cellType == CellType.FORMULA) {
formulaEvaluator.evaluateFormulaCell(cell);
cellType = cell.getCachedFormulaResultType();
}
String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);
htmlStringBuilder.append("<td>").append(cellValue).append("</td>");
}
htmlStringBuilder.append("</tr>");
}
htmlStringBuilder.append("</table></body></html>");
return htmlStringBuilder.toString();
}
返回的html字符串:
<html><head><title>Excel to HTML using Java and POI library</title><style>table, th, td { border: 1px solid black; }</style></head><body><table><tr><td>序號(hào)</td><td>姓名</td><td>性別</td><td>聯(lián)系方式</td><td>地址</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號(hào)</td></tr></table></body></html>
public String excelToHtmlStr(String excelPath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(excelPath);
try (Workbook workbook = WorkbookFactory.create(new File(excelPath))){
DataFormatter dataFormatter = new DataFormatter();
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
StringBuilder htmlStringBuilder = new StringBuilder();
htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");
htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");
htmlStringBuilder.append("</head><body><table>");
for (Row row : sheet) {
htmlStringBuilder.append("<tr>");
for (Cell cell : row) {
CellType cellType = cell.getCellType();
if (cellType == CellType.FORMULA) {
formulaEvaluator.evaluateFormulaCell(cell);
cellType = cell.getCachedFormulaResultType();
}
String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);
htmlStringBuilder.append("<td>").append(cellValue).append("</td>");
}
htmlStringBuilder.append("</tr>");
}
htmlStringBuilder.append("</table></body></html>");
return htmlStringBuilder.toString();
}
}
public String excelToHtmlStr(String excelPath) throws Exception {
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Workbook workbook = new Workbook();
workbook.loadFromFile(excelPath);
workbook.saveToStream(outputStream, com.spire.xls.FileFormat.HTML);
return outputStream.toString();
}
}
有時(shí)我們是需要的不僅僅返回html字符串,而是需要生成一個(gè)html文件這時(shí)應(yīng)該怎么做呢?一個(gè)改動(dòng)量小的做法就是使用org.apache.commons.io包下的FileUtils工具類(lèi)寫(xiě)入目標(biāo)地址:
首先需要引入pom:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
相關(guān)代碼:
String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\書(shū)籍\\電子書(shū)\\小說(shuō)\\歷史小說(shuō)\\最后的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,還可以對(duì)上面的代碼進(jìn)行一些調(diào)整,已實(shí)現(xiàn)生成html文件,代碼調(diào)整如下:
word原文件效果:
public static void wordToHtml(String wordPath, String htmlPath) {
try {
File sourceFile = new File(wordPath);
String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
File file = new File(path); // 新建一個(gè)空白pdf文檔
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); // Address是將要被轉(zhuǎn)化的word文檔
HtmlSaveOptions options = new HtmlSaveOptions();
options.setExportImagesAsBase64(true);
options.setExportRelativeFontSize(true);
doc.save(os, options);
} catch (Exception e) {
e.printStackTrace();
}
}
轉(zhuǎn)換成html的效果:
public void wordToHtml(String wordPath, String htmlPath) throws TransformerException, IOException, ParserConfigurationException {
htmlPath = FileUtil.getNewFileFullPath(wordPath, htmlPath, "html");
String ext = wordPath.substring(wordPath.lastIndexOf("."));
if (ext.equals(".docx")) {
word2007ToHtml(wordPath, htmlPath);
} else if (ext.equals(".doc")){
word2003ToHtml(wordPath, htmlPath);
} else {
throw new RuntimeException("文件格式不正確");
}
}
public void word2007ToHtml(String wordPath, String htmlPath) throws TransformerException, IOException, ParserConfigurationException {
//try(OutputStream out = Files.newOutputStream(Paths.get(path))){
try(FileOutputStream out = new FileOutputStream(htmlPath)){
word2007ToHtmlOutputStream(wordPath, out);
}
}
private void word2007ToHtmlOutputStream(String wordPath,OutputStream out) throws IOException {
ZipSecureFile.setMinInflateRatio(-1.0d);
InputStream in = Files.newInputStream(Paths.get(wordPath));
XWPFDocument document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create().setIgnoreStylesIfUnused(false).setImageManager(new Base64EmbedImgManager());
// 使用內(nèi)存輸出流
XHTMLConverter.getInstance().convert(document, out, options);
}
public void word2003ToHtml(String wordPath, String htmlPath) throws TransformerException, IOException, ParserConfigurationException {
org.w3c.dom.Document htmlDocument = word2003ToHtmlDocument(wordPath);
// 生成html文件地址
try(OutputStream outStream = Files.newOutputStream(Paths.get(htmlPath))){
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer serializer = factory.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
}
}
private org.w3c.dom.Document word2003ToHtmlDocument(String wordPath) throws IOException, ParserConfigurationException {
InputStream input = Files.newInputStream(Paths.get(wordPath));
HWPFDocument wordDocument = new HWPFDocument(input);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {
System.out.println(pictureType);
if (PictureType.UNKNOWN.equals(pictureType)) {
return null;
}
BufferedImage bufferedImage = ImgUtil.toImage(content);
String base64Img = ImgUtil.toBase64(bufferedImage, pictureType.getExtension());
// 帶圖片的word,則將圖片轉(zhuǎn)為base64編碼,保存在一個(gè)頁(yè)面中
StringBuilder sb = (new StringBuilder(base64Img.length() + "data:;base64,".length()).append("data:;base64,").append(base64Img));
return sb.toString();
});
// 解析word文檔
wordToHtmlConverter.processDocument(wordDocument);
return wordToHtmlConverter.getDocument();
}
轉(zhuǎn)換成html的效果:
public void wordToHtml(String wordPath, String htmlPath) {
htmlPath = FileUtil.getNewFileFullPath(wordPath, htmlPath, "html");
Document document = new Document();
document.loadFromFile(wordPath);
document.saveToFile(htmlPath, FileFormat.Html);
}
轉(zhuǎn)換成html的效果:
因?yàn)槭褂玫氖敲赓M(fèi)版,存在頁(yè)數(shù)和字?jǐn)?shù)限制,需要完整功能的的可以選擇付費(fèi)版本。PS:這回76頁(yè)的文檔居然轉(zhuǎn)成功了前50頁(yè)。
圖片版pdf原文件效果:
文字版pdf原文件效果:
public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
File file = new File(pdfPath);
String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new PrintWriter(path, "UTF-8");
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
}
圖片版PDF文件驗(yàn)證結(jié)果:
文字版PDF文件驗(yàn)證結(jié)果:
public void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
String path = FileUtil.getNewFileFullPath(pdfPath, htmlPath, "html");
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new PrintWriter(path, "UTF-8");
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
}
圖片版PDF文件驗(yàn)證結(jié)果:
文字版PDF原文件效果:
public void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
htmlPath = FileUtil.getNewFileFullPath(pdfPath, htmlPath, "html");
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(pdfPath);
pdf.saveToFile(htmlPath, com.spire.pdf.FileFormat.HTML);
}
圖片版PDF文件驗(yàn)證結(jié)果:
因?yàn)槭褂玫氖敲赓M(fèi)版,所以只有前三頁(yè)是正常的。。。有超過(guò)三頁(yè)需求的可以選擇付費(fèi)版本。
文字版PDF原文件效果:
報(bào)錯(cuò)了無(wú)法轉(zhuǎn)換。。。
java.lang.NullPointerException
at com.spire.pdf.PdfPageWidget.spr┢?(Unknown Source)
at com.spire.pdf.PdfPageWidget.getSize(Unknown Source)
at com.spire.pdf.PdfPageBase.spr???—(Unknown Source)
at com.spire.pdf.PdfPageBase.getActualSize(Unknown Source)
at com.spire.pdf.PdfPageBase.getSection(Unknown Source)
at com.spire.pdf.general.PdfDestination.spr︻┎?—(Unknown Source)
at com.spire.pdf.general.PdfDestination.spr┻┑?—(Unknown Source)
at com.spire.pdf.general.PdfDestination.getElement(Unknown Source)
at com.spire.pdf.primitives.PdfDictionary.setProperty(Unknown Source)
at com.spire.pdf.bookmarks.PdfBookmark.setDestination(Unknown Source)
at com.spire.pdf.bookmarks.PdfBookmarkWidget.spr┭┘?—(Unknown Source)
at com.spire.pdf.bookmarks.PdfBookmarkWidget.getDestination(Unknown Source)
at com.spire.pdf.PdfDocumentBase.spr??(Unknown Source)
at com.spire.pdf.widget.PdfPageCollection.spr┦?(Unknown Source)
at com.spire.pdf.widget.PdfPageCollection.removeAt(Unknown Source)
at com.spire.pdf.PdfDocumentBase.spr┞?(Unknown Source)
at com.spire.pdf.PdfDocument.loadFromFile(Unknown Source)
excel原文件效果:
public void excelToHtml(String excelPath, String htmlPath) throws Exception {
htmlPath = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");
Workbook workbook = new Workbook(excelPath);
com.aspose.cells.HtmlSaveOptions options = new com.aspose.cells.HtmlSaveOptions();
workbook.save(htmlPath, options);
}
轉(zhuǎn)換成html的效果:
public void excelToHtml(String excelPath, String htmlPath) throws Exception {
String path = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");
try(FileOutputStream fileOutputStream = new FileOutputStream(path)){
String htmlStr = excelToHtmlStr(excelPath);
byte[] bytes = htmlStr.getBytes();
fileOutputStream.write(bytes);
}
}
public String excelToHtmlStr(String excelPath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(excelPath);
try (Workbook workbook = WorkbookFactory.create(new File(excelPath))){
DataFormatter dataFormatter = new DataFormatter();
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
StringBuilder htmlStringBuilder = new StringBuilder();
htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");
htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");
htmlStringBuilder.append("</head><body><table>");
for (Row row : sheet) {
htmlStringBuilder.append("<tr>");
for (Cell cell : row) {
CellType cellType = cell.getCellType();
if (cellType == CellType.FORMULA) {
formulaEvaluator.evaluateFormulaCell(cell);
cellType = cell.getCachedFormulaResultType();
}
String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);
htmlStringBuilder.append("<td>").append(cellValue).append("</td>");
}
htmlStringBuilder.append("</tr>");
}
htmlStringBuilder.append("</table></body></html>");
return htmlStringBuilder.toString();
}
}
轉(zhuǎn)換成html的效果:
public void excelToHtml(String excelPath, String htmlPath) throws Exception {
htmlPath = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");
Workbook workbook = new Workbook();
workbook.loadFromFile(excelPath);
workbook.saveToFile(htmlPath, com.spire.xls.FileFormat.HTML);
}
轉(zhuǎn)換成html的效果:
從上述的效果展示我們可以發(fā)現(xiàn)其實(shí)轉(zhuǎn)成html效果不是太理想,很多細(xì)節(jié)樣式?jīng)]有還原,這其實(shí)是因?yàn)檫@類(lèi)轉(zhuǎn)換往往都是追求目標(biāo)是通過(guò)使用文檔中的語(yǔ)義信息并忽略其他細(xì)節(jié)來(lái)生成簡(jiǎn)單干凈的 HTML,所以在轉(zhuǎn)換過(guò)程中復(fù)雜樣式被忽略,比如居中、首行縮進(jìn)、字體,文本大小,顏色。舉個(gè)例子在轉(zhuǎn)換是 會(huì)將應(yīng)用標(biāo)題 1 樣式的任何段落轉(zhuǎn)換為 h1 元素,而不是嘗試完全復(fù)制標(biāo)題的樣式。所以轉(zhuǎn)成html的顯示效果往往和原文檔不太一樣。這意味著對(duì)于較復(fù)雜的文檔而言,這種轉(zhuǎn)換不太可能是完美的。但如果都是只使用簡(jiǎn)單樣式文檔或者對(duì)文檔樣式不太關(guān)心的這種方式也不妨一試。
PS:如果想要展示效果好的話,其實(shí)可以將上篇文章《文檔在線預(yù)覽(一)通過(guò)將txt、word、pdf轉(zhuǎn)成圖片實(shí)現(xiàn)在線預(yù)覽功能》說(shuō)的內(nèi)容和本文結(jié)合起來(lái)使用,即將文檔里的內(nèi)容都生成成圖片(很可能是多張圖片),然后將生成的圖片全都放到一個(gè)html頁(yè)面里 ,用html+css來(lái)保持樣式并實(shí)現(xiàn)多張圖片展示,再將html返回。開(kāi)源組件kkfilevie就是用的就是這種做法。
kkfileview展示效果如下:
下圖是kkfileview返回的html代碼,從html代碼我們可以看到kkfileview其實(shí)是將文件(txt文件除外)每頁(yè)的內(nèi)容都轉(zhuǎn)成了圖片,然后將這些圖片都嵌入到一個(gè)html里,再返回給用戶一個(gè)html頁(yè)面。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。