hongchuangyanfa 3 ani în urmă
părinte
comite
e2d7a58ece

+ 51 - 19
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/ExcelTabController.java

@@ -515,7 +515,7 @@ public class ExcelTabController extends BladeController {
             return R.fail("该数据下无此节点!");
         }
         if(wbsTreeContract.getHtmlUrl()==null){
-            return R.fail("请上传清表!");
+            return R.fail("暂无表单!");
         }
         File file1 = ResourceUtil.getFile(wbsTreeContract.getHtmlUrl());
         FileInputStream fileInputStream = new FileInputStream(file1);
@@ -1117,16 +1117,24 @@ public class ExcelTabController extends BladeController {
                 .eq(WbsTreeContract::getPKeyId, pkeyId));
 
         if(wbsTreeContract ==null ){
-            return R.fail("该数据下无此节点!");
+            return R.data(reData);
         }
         if(wbsTreeContract.getHtmlUrl()==null){
-            return R.fail("请关联清表!");
+            return R.data(reData);
+        }
+        //表单是否存储在
+        String tabName = wbsTreeContract.getInitTableName();
+        String isExitSql = " select * from information_schema.TABLES where TABLE_NAME='"+tabName+"'";
+        List<Map<String, Object>> tablist = jdbcTemplate.queryForList(isExitSql);
+        if(tablist==null || tablist.size()<=0){
+            return R.fail("无实体表对应");
         }
+
         String querySql = "select * from "+wbsTreeContract.getInitTableName()+" where p_key_id="+pkeyId ;
         List<Map<String, Object>> dataIn = jdbcTemplate.queryForList(querySql);
 
         if(dataIn==null||dataIn.size()<=0){
-            return R.fail("请填写数据!");
+            return R.data(reData);
         }
         Map<String, Object> mysqlData = dataIn.get(0);
 
@@ -1197,9 +1205,10 @@ public class ExcelTabController extends BladeController {
 
         String querySql = "select * from "+wbsTreeContract.getInitTableName()+" where p_key_id="+pkeyId ;
         List<Map<String, Object>> dataIn = jdbcTemplate.queryForList(querySql);
-        if(dataIn==null){
-            return R.fail("请填写数据!");
-        }
+
+        if(dataIn!=null && dataIn.size()>=1){
+
+
         Map<String, Object> mysqlData = dataIn.get(0);
         File file1 = ResourceUtil.getFile(wbsTreeContract.getHtmlUrl());
         String htmlString =  IoUtil.readToString(new FileInputStream(file1));
@@ -1233,26 +1242,27 @@ public class ExcelTabController extends BladeController {
                 }
             }
         }
-
+        }else {
+            File file1 = ResourceUtil.getFile(wbsTreeContract.getHtmlUrl());
+            String htmlString =  IoUtil.readToString(new FileInputStream(file1));
+            // 样式集合
+            doc = Jsoup.parse(htmlString);
+        }
         }else{
             File file1 = ResourceUtil.getFile(wbsTreeContract.getHtmlUrl());
             String htmlString =  IoUtil.readToString(new FileInputStream(file1));
             // 样式集合
             doc = Jsoup.parse(htmlString);
         }
-        String pdfPath="/Users/hongchuangyanfa/Desktop/pdf//"+pkeyId+".pdf";
-        String pdfPath_size="/Users/hongchuangyanfa/Desktop/pdf//"+pkeyId+"size.pdf";
-        String pdfHtmlPath="/Users/hongchuangyanfa/Desktop/pdfHtml//"+pkeyId+".html";
-
-        File writefile = new File(pdfHtmlPath);
-        FileUtil.writeToFile(writefile, doc.html(), Boolean.parseBoolean("UTF-8"));
-
-        HtmlToPdf html = new HtmlToPdf();
-        html.tomPdf(pdfHtmlPath,pdfPath);
 
-        PdfConvertA4Utils.convert(pdfPath,pdfPath_size);
+        String pdfPath="/Users/hongchuangyanfa/Desktop/pdf//"+pkeyId+".pdf";
+        File tabpdf = ResourceUtil.getFile(pdfPath);
+        if(tabpdf.exists()){
+            tabpdf.delete();
+        }
 
-        BladeFile bladeFile = newIOSSClient.uploadFile(pkeyId + ".pdf", pdfPath_size);
+        HtmlToPdf.tomPdf(doc,pdfPath);
+        BladeFile bladeFile = newIOSSClient.uploadFile(pkeyId + ".pdf", pdfPath);
         //
         TableFile tableFile1 = tableFileService.getBaseMapper().selectOne(Wrappers.<TableFile>query().lambda()
                 .eq(TableFile::getTabId, pkeyId).eq(TableFile::getType,1));
@@ -1315,7 +1325,14 @@ public class ExcelTabController extends BladeController {
         }
         wbsTreeContract.setDeptName(deptName);
         wbsTreeContract.setIsCopeTab(2);
+        wbsTreeContract.setIsTabPdf(1); // pdf 不能预览
+        wbsTreeContract.setIsBussShow("1"); // 是否隐藏表
+        wbsTreeContract.setTabFileType(1);//没有上传附件
         wbsTreeContractService.save(wbsTreeContract);
+
+        // 复制表数据
+
+
         return R.data("成功");
     }
 
@@ -1371,10 +1388,25 @@ public class ExcelTabController extends BladeController {
         WbsTreeContract wbsTreeContract = wbsTreeContractService.getBaseMapper().selectOne(Wrappers.<WbsTreeContract>query().lambda()
                 .eq(WbsTreeContract::getPKeyId, pkeyId));
 
+
+        //生成pdf
+        List<TableFile> tableFileList = tableFileService.getBaseMapper().selectList(Wrappers.<TableFile>query().lambda().eq(TableFile::getTabId, pkeyId));
+        tableFileList.sort(Comparator.comparing(TableFile::getType));
+
+        //
+        List<String> dataListPdf = tableFileList.stream().filter(tableFile1 -> tableFile1.getDomainPdfUrl()!=null).map(TableFile::getDomainPdfUrl).collect(Collectors.toList());
+
+        String pdfPath2 = "/Users/hongchuangyanfa/Desktop/pdf//"+pkeyId+"_2.pdf";
+
+        FileUtils.mergePdfPublicMethods(dataListPdf,pdfPath2);
+
+        BladeFile bladeFile2 = newIOSSClient.uploadFile(pkeyId + "2.pdf", pdfPath2);
+
         UpdateWrapper<WbsTreeContract> updateWrapper = new UpdateWrapper<>();
         updateWrapper.in("p_key_id",pkeyId);
         updateWrapper.set("tab_file_type",2);
         updateWrapper.set("is_tab_pdf",2);
+        updateWrapper.set("pdf_url",bladeFile2.getLink());
         wbsTreeContractService.update(updateWrapper);
 
         return R.data(tableFile.getId());

+ 81 - 11
blade-service/blade-manager/src/main/java/org/springblade/manager/unit/HtmlToPdf.java

@@ -4,16 +4,12 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
+import com.alibaba.fastjson.JSONObject;
 import com.itextpdf.html2pdf.ConverterProperties;
 import com.itextpdf.html2pdf.HtmlConverter;
 import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
 import com.itextpdf.kernel.colors.Color;
-import com.itextpdf.kernel.geom.PageSize;
 import com.itextpdf.kernel.geom.Rectangle;
 import com.itextpdf.kernel.pdf.PdfDocument;
 import com.itextpdf.kernel.pdf.PdfPage;
@@ -22,7 +18,6 @@ import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
 import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
 import com.itextpdf.layout.Document;
 import com.itextpdf.layout.element.LineSeparator;
-import com.itextpdf.text.RectangleReadOnly;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
@@ -39,7 +34,59 @@ public class HtmlToPdf {
 	private final static String SRC = "/Users/hongchuangyanfa/Desktop/privateUrl/1550366339149856768.html";
 	public static final String FONT = "/Users/hongchuangyanfa/tool/NotoSansCJKsc-Regular.otf";
 
-	public void tomPdf(String html, String DEST) throws FileNotFoundException, IOException {
+	public static void tomPdf(org.jsoup.nodes.Document doc, String DEST) throws FileNotFoundException, IOException {
+
+		Element style = doc.select("style").first();
+
+
+		Element table = doc.select("table").first();
+
+		Elements col = doc.select("Col");
+		Elements trs = table.select("tr");
+		float html_max_width = 0;
+		float html_max_heght = 0 ;
+		for(int i = 0 ; i <col.size();i++){
+			Element element = col.get(i);
+			html_max_width += Integer.parseInt(element.attr("width").replaceAll("px",""));
+		}
+
+		for (int i = 0 ;i<trs.size();i++){
+			Element tr = trs.get(i);
+			Elements tds = tr.select("td");
+			if(tds.size()>=1){
+				String heg[] =tds.get(0).attr("style").split(";");
+				for (String str:heg){
+					if(str.indexOf("height:")>=0){
+						html_max_heght += Integer.parseInt(str.split(":")[1].replaceAll("px",""));
+					}
+				}
+			}
+		}
+		System.out.println(html_max_heght+"_"+html_max_width);
+		if(html_max_heght>html_max_width){
+			html_max_heght+=250;
+			style.append(" @page{size:"+html_max_width+"px "+html_max_heght+"px} table{page-break-inside:avoid;}");
+		}else{
+			html_max_width+=100;
+			if(html_max_heght<-650){
+				html_max_heght = html_max_heght*2-400;
+			}else{
+				html_max_heght+=250;
+			}
+			style.append(" @page{size:"+html_max_width+"px "+html_max_heght+"px} table{page-break-inside:avoid;}");
+		}
+
+
+		ConverterProperties props = new ConverterProperties();
+		DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
+		defaultFontProvider.addFont(FONT);
+		props.setFontProvider(defaultFontProvider);
+		PdfWriter writer = new PdfWriter(DEST);
+		HtmlConverter.convertToPdf(doc.html(),writer,props);
+	}
+
+
+/*	public void tomPdf(String html, String DEST) throws FileNotFoundException, IOException {
 
 		File file1 = ResourceUtil.getFile(html);
 		String htmlString =  IoUtil.readToString(new FileInputStream(file1));
@@ -56,7 +103,6 @@ public class HtmlToPdf {
 			html_max_width += Integer.parseInt(element.attr("width").replaceAll("px",""));
 		}
 
-
 		ConverterProperties props = new ConverterProperties();
 		DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
 		defaultFontProvider.addFont(FONT);
@@ -64,14 +110,18 @@ public class HtmlToPdf {
 		PdfWriter writer = new PdfWriter(DEST);
 		PdfDocument pdf = new PdfDocument(writer);
 		if(html_max_width>html_max_heght){
+			//html_max_width = 1123;
+			html_max_heght = 794;
 			html_max_width = 1123;
 			html_max_heght = 794;
 		}else{
 			html_max_width = 794;
 			html_max_heght = 1123;
 		}
-		pdf.setDefaultPageSize(new PageSize(html_max_width-200, html_max_heght));
+		pdf.setDefaultPageSize(new PageSize(html_max_width, html_max_heght));
 		Document document = HtmlConverter.convertToDocument(new FileInputStream(html), pdf, props);
+
+		HtmlConverter.
 		// 将所有内容在一个页面显示
 		EndPosition endPosition = new EndPosition();
 		LineSeparator separator = new LineSeparator(endPosition);
@@ -79,12 +129,32 @@ public class HtmlToPdf {
 		document.getRenderer().close();
 		PdfPage page = pdf.getPage(1);
 		float y = endPosition.getY() - 36;
-		page.setMediaBox(new Rectangle(0, y, html_max_width-200, html_max_heght));
+		page.setMediaBox(new Rectangle(0, 0, html_max_width, html_max_heght-y));
 		document.close();
 		pdf.close();
-	}
+	}*/
+
+	public static void main22(String[] args) throws IOException, InterruptedException {
+		HtmlToPdf html = new HtmlToPdf();
+
+		// shu
+		File file1 = ResourceUtil.getFile("/Users/hongchuangyanfa/Desktop/privateUrl/1557303077508218880.html");
+		String htmlString =  IoUtil.readToString(new FileInputStream(file1));
+		org.jsoup.nodes.Document doc = Jsoup.parse(htmlString);
 
 
+		String pdfPath= "/Users/hongchuangyanfa/Desktop/pdfHtml/1556911621165547526222xxx.pdf";
+		File tabpdf = ResourceUtil.getFile(pdfPath);
+		if(tabpdf.exists()){
+			tabpdf.delete();
+		}
+		Thread.sleep(200);
+		//html.tomPdf(doc.html(),pdfPath);
+
+		//hen
+		//html.tomPdf("/Users/hongchuangyanfa/Desktop/pdfHtml/1556603810330181634.html","/Users/hongchuangyanfa/Desktop/pdfHtml/1556911621165547526222.pdf");
+	}
+
 	class EndPosition implements ILineDrawer {
 
 		/** A Y-position. */