公司一個小項目要結尾了, 有很是多的表格, 並且很是複雜, 例如html
例如:(這表格, 有想死的心…)
前端
看到這樣的表格,第一反應是n臉懵逼,這得作到啥時候啊,最後想到用一個方法,讓客戶本身玩!!
平臺提供Excel模版下載,客戶下載填寫完再提交Excel,平臺再將Excel轉PDF,順便將PDF轉成圖片,給前端展現,想法簡單,方案可行,開工!java
本文選用openoffice,各位看官可自選用iText + poi,本身判別!固然也能夠用jacob,前提是你不須要在Linux中運行項目。linux
進入到安裝目錄,若是路徑是默認的,則在 C:\Program Files (x86)\OpenOffice 4\program 下面,
進到這個路徑下,按住 shift 再點擊鼠標右鍵,在此處打開命令窗口,輸入 soffice -headless-accept=「socket,host=127.0.0.1,port=8100;urp;」-nofirststartwizard, 查看是否啓動成功, 黑窗口輸入 netstat -ano
windows下安裝完畢!git
參考: https://blog.csdn.net/qq_30987095/article/details/78789103github
[root@iZ2zeg3etf08f6snef3lgrZ ~]# lsb_release -a LSB Version: :(略) Distributor ID: CentOS Description: CentOS Linux release (略) Release: (略) Codename: Core
下載完畢後,利用ssh工具上傳到服務器上!web
> tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_install-rpm_zh-CN.tar.gz (略)...... > ls zh-CN > cd zh-CN/RPMS > ls openoffice-core01-4.1.3-9783.x86_64.rpm openoffice-impress-4.1.3-9783.x86_64.rpm openoffice-zh-CN-4.1.3-9783.x86_64.rpm openoffice-4.1.3-9783.x86_64.rpm openoffice-core02-4.1.3-9783.x86_64.rpm openoffice-javafilter-4.1.3-9783.x86_64.rpm openoffice-zh-CN-base-4.1.3-9783.x86_64.rpm openoffice-base-4.1.3-9783.x86_64.rpm openoffice-core03-4.1.3-9783.x86_64.rpm openoffice-math-4.1.3-9783.x86_64.rpm openoffice-zh-CN-calc-4.1.3-9783.x86_64.rpm openoffice-brand-base-4.1.3-9783.x86_64.rpm openoffice-core04-4.1.3-9783.x86_64.rpm openoffice-ogltrans-4.1.3-9783.x86_64.rpm openoffice-zh-CN-draw-4.1.3-9783.x86_64.rpm openoffice-brand-calc-4.1.3-9783.x86_64.rpm openoffice-core05-4.1.3-9783.x86_64.rpm openoffice-onlineupdate-4.1.3-9783.x86_64.rpm openoffice-zh-CN-help-4.1.3-9783.x86_64.rpm openoffice-brand-draw-4.1.3-9783.x86_64.rpm openoffice-core06-4.1.3-9783.x86_64.rpm openoffice-ooofonts-4.1.3-9783.x86_64.rpm openoffice-zh-CN-impress-4.1.3-9783.x86_64.rpm openoffice-brand-impress-4.1.3-9783.x86_64.rpm openoffice-core07-4.1.3-9783.x86_64.rpm openoffice-ooolinguistic-4.1.3-9783.x86_64.rpm openoffice-zh-CN-math-4.1.3-9783.x86_64.rpm openoffice-brand-math-4.1.3-9783.x86_64.rpm openoffice-draw-4.1.3-9783.x86_64.rpm openoffice-pyuno-4.1.3-9783.x86_64.rpm openoffice-zh-CN-res-4.1.3-9783.x86_64.rpm openoffice-brand-writer-4.1.3-9783.x86_64.rpm openoffice-gnome-integration-4.1.3-9783.x86_64.rpm openoffice-ure-4.1.3-9783.x86_64.rpm openoffice-zh-CN-writer-4.1.3-9783.x86_64.rpm openoffice-brand-zh-CN-4.1.3-9783.x86_64.rpm openoffice-graphicfilter-4.1.3-9783.x86_64.rpm openoffice-writer-4.1.3-9783.x86_64.rpm openoffice-calc-4.1.3-9783.x86_64.rpm openoffice-images-4.1.3-9783.x86_64.rpm openoffice-xsltfilter-4.1.3-9783.x86_64.rpm [註釋:運行下面命令, 生成 desktop-integration包] > yum localinstall *.rpm > (略...) > cd desktop-integration/ > ls > openoffice4.1.3-freedesktop-menus-4.1.3-9783.noarch.rpm openoffice4.1.3-mandriva-menus-4.1.3-9783.noarch.rpm openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm openoffice4.1.3-suse-menus-4.1.3-9783.noarch.rpm > yum localinstall openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm
ok, 安裝完畢了! 啓動服務
進入到默認安裝目錄下apache
> cd /opt/openoffice4/program/ [註釋:臨時啓動] > /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard [註釋:或後臺啓動] > /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & [註釋:永久啓動] > nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & [註釋:查看是否啓動成功] > ps -ef|grep openoffice root 7541 7527 0 10:19 pts/0 00:00:12 /opt/openoffice4/program/soffice.bin -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard root 8964 7066 0 18:18 pts/3 00:00:00 grep --color=auto openoffice 或者 > netstat -tunlp |grep tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 7541/soffice.bin
Linux下安裝成功!ubuntu
開始擼碼:windows
<!-- openoffice 相關依賴 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.2.1</version> </dependency> <!-- pdf2img --> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-tools</artifactId> <version>2.0.6</version> </dependency>
import java.awt.*; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.net.ConnectException; import org.apache.commons.lang.StringUtils; import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.DocumentFormatRegistry; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageTree; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.imageio.ImageIOUtil; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; /** * * @author josnow * @date 2017年5月9日 下午12:38:39 * @version 1.0.0 * @desc openoffice轉換工具 */ public class OpenOfficeUtils { public static final String LOCAL_HOST = "localhost"; public static final int LOCAL_PORT = 8100; // Format public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry(); /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:11:04 * @param inputFilePath * 待轉換的文件路徑 * @param outputFilePath * 輸出文件路徑 */ public static void convert(String inputFilePath, String outputFilePath) throws ConnectException { convert(inputFilePath, outputFilePath, LOCAL_HOST, LOCAL_PORT); } /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:12:29 * @param inputFilePath * 待轉換的文件路徑 * @param outputFilePath * 輸出文件路徑 * @param connectIp * 遠程調用ip * @param connectPort * 遠程調用端口 */ public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort) throws ConnectException { if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath) || StringUtils.isEmpty(connectIp)) { throw new IllegalArgumentException("參數異常!!"); } OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort); connection.connect(); // TODO Excel轉成PDF默認是A4紙 // DocumentConverter converter = getConverter(connectIp, connection); // converter.convert(new File(inputFilePath), new File(outputFilePath)); // TODO Excel轉成PDF默認是A4紙, 若是現實折行,則自定義ConverterDocument,改變紙張大小 ConverterDocument converterDocument = new ConverterDocument(connection); converterDocument.convert(new File(inputFilePath), new File(outputFilePath)); connection.disconnect(); } /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:08:26 * @param inputStream * @param inputFileExtension * 待轉換文件的擴展名,例如: xls,doc * @param outputStream * @param outputFileExtension * 輸出文件擴展名,例如:pdf */ public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream, String outputFileExtension) throws ConnectException { convert(inputStream, inputFileExtension, outputStream, outputFileExtension, LOCAL_HOST, LOCAL_PORT); } /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:10:21 * @param inputStream * @param inputFileExtension * 待轉換文件的擴展名,例如: xls,doc * @param outputStream * @param outputFileExtension * 輸出文件擴展名,例如:pdf * @param connectIp * 遠程調用ip * @param connectPort * 遠程調用端口 */ public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream, String outputFileExtension, String connectIp, int connectPort) throws ConnectException { if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null || StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) { throw new IllegalArgumentException("參數異常!!"); } OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort); connection.connect(); DocumentConverter converter = getConverter(connectIp, connection); converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream, formatFactory.getFormatByFileExtension(outputFileExtension)); connection.disconnect(); } private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) { DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp) || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection) : new StreamOpenOfficeDocumentConverter(connection); return converter; } public static void main(String[] args) throws Exception { OpenOfficeUtils.convert("D:\\test.xls", "D:\\test.pdf"); pdf2image(new File("D:\\test.pdf")); } /** * pdf to big image * github: https://github.com/mmtou/pdf2img * @param pdfFile pdf文件路徑 * @return big image file * @throws Exception */ public static File pdf2image(File pdfFile) throws Exception { // 拼成圖片後的寬度和高度 int w = 0; int h = 0; List<BufferedImage> images = new ArrayList(); // 生成圖片後的路徑 String path = pdfFile.getParent() + File.separator; String fileName = pdfFile.getName().replace(".pdf", ""); File destinationFile = new File(path); if (!destinationFile.exists()) { destinationFile.mkdir(); } PDDocument document = PDDocument.load(pdfFile); PDPageTree list = document.getDocumentCatalog().getPages(); int pageCounter = 0; for (PDPage page : list) { PDFRenderer pdfRenderer = new PDFRenderer(document); BufferedImage image = pdfRenderer.renderImageWithDPI(pageCounter, 100, ImageType.RGB); String target = path + fileName + "-" + (pageCounter++) + ".png"; ImageIOUtil.writeImage(image, target, 100); w = image.getWidth(); h += image.getHeight(); images.add(image); new File(target).delete(); } document.close(); BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics g = combined.getGraphics(); int y = 0; for (BufferedImage image : images) { g.drawImage(image, 0, y, null); y += image.getHeight(); } // Save as new image File image = new File(path, fileName + ".png"); ImageIO.write(combined, "PNG", image); return image; } }
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; import com.sun.star.awt.Size; import com.sun.star.beans.PropertyValue; import com.sun.star.lang.XComponent; import com.sun.star.uno.UnoRuntime; import com.sun.star.view.PaperFormat; import com.sun.star.view.XPrintable; public class ConverterDocument extends StreamOpenOfficeDocumentConverter { public ConverterDocument(OpenOfficeConnection connection) { super(connection); } public final static Size A5, A4, A3; public final static Size B4, B5, B6; public final static Size KaoqinReport; static { A5 = new Size(14800, 21000); A4 = new Size(21000, 29700); A3 = new Size(29700, 42000); B4 = new Size(25000, 35300); B5 = new Size(17600, 25000); B6 = new Size(12500, 17600); // KaoqinReport = new Size(29700, 27940); //最大限度 寬 1600000 KaoqinReport = new Size(25000, 21000);; } @Override protected void refreshDocument(XComponent document) { super.refreshDocument(document); // The default paper format and orientation is A4 and portrait. To // change paper orientation // re set page size XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document); PropertyValue[] printerDesc = new PropertyValue[2]; // Paper Orientation // printerDesc[0] = new PropertyValue(); // printerDesc[0].Name = "PaperOrientation"; // printerDesc[0].Value = PaperOrientation.PORTRAIT; // Paper Format printerDesc[0] = new PropertyValue(); printerDesc[0].Name = "PaperFormat"; printerDesc[0].Value = PaperFormat.USER; // Paper Size printerDesc[1] = new PropertyValue(); printerDesc[1].Name = "PaperSize"; printerDesc[1].Value = KaoqinReport; try { xPrintable.setPrinter(printerDesc); } catch (Exception e) { e.printStackTrace(); } } }
public static void main(String[] args) throws Exception { OpenOfficeUtils.convert("D:\\test.xls", "D:\\test.pdf"); pdf2image(new File("D:\\test.pdf")); }
參考: https://blog.csdn.net/liuhualiang/article/details/14094019?utm_source=blogxgwz6