1. 程式人生 > >WEB工程生成PDF並匯出到本地

WEB工程生成PDF並匯出到本地

package com.powersi;


import java.io.File;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.powersi.Test;
import com.powersi.hygeia.framework.ParameterMapping;
import com.powersi.hygeia.framework.util.DBHelper;
import com.powersi.hygeia.framework.util.UtilFunc;


public class Test {




    Document document = null;// 建立一個Document物件      
    private static Font headFont ;
    private static Font keyFont ;
    private static Font nbspFont;
    private static Font textfont_H ;
    private static Font textfont_B ;
    private static Font textfont_13;
    private static Font textfont_12;
    private static Font textfont_11;
    private static Font textfont_10;
    private static Font textfont_9;
    private static Font textfont_8;
    private static Font textfont_7;
    private static Font textfont_6;
    int maxWidth = 520;


    static{    
        BaseFont bfChinese_H;
        try {    
            /** 
             * 新建一個字型,iText的方法 STSongStd-Light 是字型,在iTextAsian.jar 中以property為字尾 
             * UniGB-UCS2-H 是編碼,在iTextAsian.jar 中以cmap為字尾 H 代表文字版式是 橫版, 相應的 V 代表豎版  
             */
            bfChinese_H = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    


            headFont = new Font(bfChinese_H, 14, Font.NORMAL);   
            keyFont = new Font(bfChinese_H, 26, Font.BOLD); 
            nbspFont = new Font(bfChinese_H, 13, Font.NORMAL);
            textfont_H = new Font(bfChinese_H, 10, Font.NORMAL); 
            textfont_B = new Font(bfChinese_H, 12, Font.NORMAL); 
            textfont_13 = new Font(bfChinese_H, 13, Font.NORMAL);  
            textfont_12 = new Font(bfChinese_H, 12, Font.NORMAL); 
            textfont_11 = new Font(bfChinese_H, 11, Font.NORMAL); 
            textfont_10 = new Font(bfChinese_H, 10, Font.NORMAL); 
            textfont_9 = new Font(bfChinese_H, 9, Font.NORMAL); 
            textfont_8 = new Font(bfChinese_H, 8, Font.NORMAL); 
            textfont_7 = new Font(bfChinese_H, 7, Font.NORMAL);
            textfont_6 = new Font(bfChinese_H, 6, Font.NORMAL); 


        } catch (Exception e) {             
            e.printStackTrace();    
        }     
    }


    /**
     * 設定頁面屬性
     * @param file
     */
    public Test(File file) {


        //自定義紙張
        Rectangle rectPageSize = new Rectangle(350, 620);


        // 定義A4頁面大小
        //Rectangle rectPageSize = new Rectangle(PageSize.A4);  
        rectPageSize = rectPageSize.rotate();// 加上這句可以實現頁面的橫置
        document = new Document(rectPageSize,10, 150, 10, 40);


        try {
            PdfWriter.getInstance(document,new FileOutputStream(file));    
            document.open();
        } catch (Exception e) {
            e.printStackTrace();    
        }
    }


    /**
     * 建表格(以列的數量建)
     * @param colNumber
     * @return
     */
    public PdfPTable createTable(int colNumber){    
        PdfPTable table = new PdfPTable(colNumber);    
        try{    
            //table.setTotalWidth(maxWidth);    
            //table.setLockedWidth(true);    
            table.setHorizontalAlignment(Element.ALIGN_CENTER);         
            table.getDefaultCell().setBorder(1); 
            table.setSpacingBefore(10);
            table.setWidthPercentage(100);
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return table;
    }


    /**
     * 建表格(以列的寬度比建)
     * @param widths
     * @return
     */
    public PdfPTable createTable(float[] widths){    
        PdfPTable table = new PdfPTable(widths);    
        try{    
            //table.setTotalWidth(maxWidth);    
            //table.setLockedWidth(true);    
            table.setHorizontalAlignment(Element.ALIGN_CENTER);         
            table.getDefaultCell().setBorder(1); 
            table.setSpacingBefore(10);
            table.setWidthPercentage(100);
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return table;    
    }




    /**
     * 表格中單元格
     * @param value
     * @param font
     * @param align
     * @return
     */
    public PdfPCell createCell(String value,Font font,int align){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);            
        cell.setHorizontalAlignment(align);        
        cell.setPhrase(new Phrase(value,font));   
        
        return cell;    
    }


    /**
     * 表格中單元格
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowspan
     * @return
     */
    public PdfPCell createCell(String value,Font font,int align_v,int align_h,int colspan,int rowspan){
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(align_v);    
        cell.setHorizontalAlignment(align_h);        
        cell.setColspan(colspan); 
        cell.setRowspan(rowspan); 
        cell.setPhrase(new Phrase(value,font));  
        return cell;
    }
    /**
     * 建立無邊框表格
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowspan
     * @return
     */
    public PdfPCell createCellNotBorder(String value,Font font,int align_v,int align_h,int colspan,int rowspan){
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(align_v);    
        cell.setHorizontalAlignment(align_h);        
        cell.setColspan(colspan); 
        cell.setRowspan(rowspan); 
        cell.setPhrase(new Phrase(value,font));  
        cell.setBorderWidth(0f);
        return cell;
    }


    /**
     * 建短語
     * @param value
     * @param font
     * @return
     */
    public Phrase createPhrase(String value,Font font){ 
        Phrase phrase = new Phrase();
        phrase.add(value);
        phrase.setFont(font);
        return phrase;
    }  


    /**
     * 建段落
     * @param value
     * @param font
     * @param align
     * @return
     */
    public Paragraph createParagraph(String value,Font font,int align){ 
        Paragraph paragraph = new Paragraph();
        paragraph.add(new Phrase(value,font));
        paragraph.setAlignment(align);
        return paragraph;
    }
    
    public void generatePDF(String tjid) throws Exception{


    document.add(createParagraph("機構名稱",headFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
    document.add(createParagraph("文件名稱",keyFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
   
    //表格資訊
        float[] widths1 = {20f,15f,10f,10f,20f,8f,9f,8f};   //整個表格的列寬,列數。
        PdfPTable table1 = createTable(widths1);   
        // 內容  字型垂直對齊  橫向對齊                                佔列     佔行
        table1.addCell(createCell("體檢號碼:", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("姓名", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("婚姻", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("性別", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("年齡", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        
        table1.addCell(createCell("證件號碼", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1)); 
        table1.addCell(createCell("聯絡電話", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
        
        table1.addCell(createCell("體檢型別", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1)); 
        table1.addCell(createCell("體檢日期", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
        
        table1.addCell(createCell("單位名稱", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,7,1));
        document.add(table1); 
   
        
        
        document.close();
    }


    public static void main(String[] args) throws Exception {
        File file = new File("D:\\test.pdf");
        file.createNewFile();
        String tjid="";
       new Test(file).generatePDF(tjid); 
    }


}

相關推薦

WEB工程生成PDF匯出本地

package com.powersi;import java.io.File;import java.io.FileOutputStream;import java.sql.Blob;import java.util.LinkedHashMap;import java.ut

Java之生成PdfPdf內容操作

enc images sub als tar 應用 throw mave add 雖說網上有很多可以在線導出Pdf或者word或者轉成png等格式的工具,但是我覺得還是得了解知道是怎麽實現的。一來,在線免費轉換工具,是有容量限制的,達到一定的容量時,是不能成功導出的;二來,

PHP生成PDF轉換成圖片爬過的坑

  需求描述:根據訂單通過模板合同生成新的PDF合同通過e籤寶簽約後轉為圖片給使用者下載。   需求整理:     1.如何生成PDF檔案:使用TCPDF擴充套件生成。思考:       ⑴為了方便將模板中的固定佔位符替換為訂單中的內容,使用靜態html頁面儲存合同模板的方式儲存       ⑵使用T

生成pdf展示出來

前端請求程式碼: function preview(custcode,tempId){ layer.open({ type: 2, title: '簡歷預覽', maxmin: true,

itext應用HTML模版生成PDF加水印,處理了中文問題

剛用itext實現了一個生成PDF加水印 其中遇到的問題是它對中文支援不好,專門寫了對中文的處理 先看效果圖: maven包 <dependency> <groupId>

基於ABP做一個簡單的系統——實戰篇:4.基於富文字編輯器,Razor模板引擎生成內容匯出Word 填坑記錄

起因 需求是這樣的,有一種協議需要生成,協議的模板是可配置的,在生成過程中,模板中的內容可以根據約定的標記進行替換(就像mvc的razor模板一樣)。生成後的內容還需要匯出成word或pdf。 常見的使用場景比如租賃協議生成,郵件內容模板生成等等,不要傻傻的hard-code像‘#name#’這樣的標記了。

java根據模板生成pdf檔案匯出(轉)

import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.

maven如何在web工程中將本地lib文件打包到war中

編譯打包 edi dir rec 三方 nbsp code sco conf 在某些項目中可能會出現需要引用第三方商業化SDK的場景,此類商業SDK可能並不會納入maven中心倉庫,所以在編譯打包過程中需要引用本地lib包。 1、引用本地jar包編譯 <depend

在.net Core 使用PDF模板文件生成PDF文件,代替WEB打印控件!

cnblogs bat dex make io流 控件 文本框 找到 public 這幾天找WEB打印控件,要麽收費的,要麽免費的只能在IE裏用! 我只想簡單的打個標簽紙!百度2天,看到一老兄說可以用PDF,然後又開始百度..找到了一篇文章 http://www.jians

根據圖片的URL生成PDF保存到本地(前臺js)

避免 就會 需要 uri con bug 是個 tput llb //需要引用html2canvas.js和jspdf.js SavePDF:function(e){ var that = this; $.post("/Biz/D

java匯入excle表格,並且對錶格進行相應的修改,對錶格資料進行整理,最後匯出本地表格等一系列操作

1.首先建立一個java專案 2.匯入以下jar包 3.程式碼如下   public class auto_date { private static List<List<String>> readExcel(File file) throws Exception

讀取web工程目錄之外的圖片顯示

可以有2種方式,1:目錄對映,2:讀取流 方式1: 需要在conf目錄下對server.xml做修改,就是在<host></host>標籤之間加入<Context path="images" docBase="D:\pictureFiles" debug="0"/> &

福利!NodeJs爬取網路教程生成PDF檔案,以阮一峰JavaScript教程和ES6教程為例(附原始碼和PDF檔案)

前言 你想一夜暴富嗎?你想一夜成名嗎?你想開蘭博基尼泡妞嗎?你想拿鈔票點菸嗎?你想成為世界主宰嗎?那麼,趕緊往下看吧,雖然它不能達成前面所說的任 何一個夢想,但是,你將獲得: 通過命令列將某網站的內容轉成PDF檔案 通過NodeJS爬蟲將某網路教程(例如阮一峰的JavaScript教程和ES6教

9.idea中基於tomcat部署web工程設定為熱部署

1.點選Edit configurations 選擇Deployment --》點選+ -》然後新增exploded結尾的那個 2.做如下配置  不做第一步配置沒有下面的Update classes and resources選項  設定編碼 -Dfile

Java生成匯出Json檔案

將一個list集合轉換成json檔案並匯出:      資料集合:     List<Object> agencyList = new ArrayList<Object>(); Map<String, Object> agencyMap

java匯入excle表格,並且對錶格進行相應的修改,對錶格資料進行整理,最後匯出本地表格等一系列

1.首先建立一個java專案   完成效果如下圖所示 2.匯入以下jar包 3.程式碼如下   其中行和列的操作是根據需求自動劃分的 複製程式碼1 public class auto_date {2 private static List<List<String>> read

echart生成報表生成可下載PDF

 Echarts 一個純javascript的圖表庫,而且Echart3開始不再強制使用AMD的方式按需引入,程式碼立也不再內建AMD載入器,只需要像普通的javaScript庫一樣用script標籤引入.   文件和下載地址: echarts html2

使用poi將ueditor生成的內容匯出生成word文件,以頁面檢視開啟

 將ueditor生成的內容儲存成字串,通過下面的內容拼成html,並使生成的word以頁面檢視的方式開啟。 String str = " <!--[if gte mso 9]><xml><w:WordDocument><w:

Java檔案上傳資料庫(儲存本地)、word轉pdf進行頁面預覽

對於頁面預覽用到了OpenOffice附件: 官方的下載地址:Apache OpenOffice 選擇windows版本安裝完成後,在cmd中執行下面兩個命令,檢視工作管理員中是否有soffice.bin的程序。(用到OpenOffice,必須保證工作管理員中有

【優化版】Java檔案上傳資料庫(儲存本地)、word轉pdf進行頁面預覽

上一篇檔案上傳【點選跳轉】,是將路徑等檔案資訊存進log_file臨時表,內容二進位制存入資料庫Test表,這種邏輯是在呼叫資料庫表Test內容展示時,判斷檔案為word(轉換成pdf)還是pdf(直接展示)。 上一篇連結:連結地址。 下面進一步優化: 具體邏輯