ssm+maven實現pdf導出

第一步:根據需求新建一個word文檔

第二步:下載Adobe Acrobat DC並安裝

第三步:用Adobe Acrobat DC新建pdf文件。如圖:

第四步:創建form域。a.當word文檔以表格形式就不需要再自己額外添加域了;b.當word文檔沒有表格,需要自己添加域,如下圖進行添加域。(這裏的域指的是需要被替換的位置)

第五步:pom.xml中加入

<!--pdf 這個jar包包含了字體  -->
		
        <dependency>
        <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

    工具類PDFTempletTicket:

/**
 * 
 */
package org.csun.nc.utils;

/**
 * @author liqian
 * @date   2018年3月26日
 * @time   上午10:41:32
 * Description : 
 */

import org.csun.nc.pojo.Advisory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

/**
 * @ClassName: PDFTempletTicket
 * @Description: TODO
 * @Author: 
 * @Date: 
 */
public class PDFTempletTicket {

	private String templatePdfPath;
	private String ttcPath;
	private String targetPdfpath;
	private Advisory advisory;

	public PDFTempletTicket() {
		super();
	}

	public PDFTempletTicket(String templatePdfPath, String ttcPath,
			String targetPdfpath, Advisory advisory) {
		this.templatePdfPath = templatePdfPath;
		this.ttcPath = ttcPath;
		this.targetPdfpath = targetPdfpath;
		this.advisory = advisory;
	}

	public void templetTicket(File file) throws Exception {
		PdfReader reader = new PdfReader(templatePdfPath);

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		PdfStamper ps = new PdfStamper(reader, bos);

		AcroFields s = ps.getAcroFields();

		s.setField("address", advisory.getAddress());
		s.setField("advisoryName", advisory.getAdvisoryName());
		s.setField("advisoryPhone", advisory.getAdvisoryPhone());
		s.setField("oldName", advisory.getOldName());
		s.setField("receptionist", advisory.getReceptionist());
		
		ps.setFormFlattening(true);
		ps.close();

		FileOutputStream fos = new FileOutputStream(file);
		fos.write(bos.toByteArray());
		fos.close();
	}

	/**
	 * @return the templatePdfPath
	 */
	public String getTemplatePdfPath() {
		return templatePdfPath;
	}

	/**
	 * @param templatePdfPath
	 *            the templatePdfPathto set
	 */
	public void setTemplatePdfPath(String templatePdfPath) {
		this.templatePdfPath = templatePdfPath;
	}

	/**
	 * @return the ttcPath
	 */
	public String getTtcPath() {
		return ttcPath;
	}

	/**
	 * @param ttcPath
	 *            the ttcPath to set
	 */
	public void setTtcPath(String ttcPath) {
		this.ttcPath = ttcPath;
	}

	/**
	 * @return the targetPdfpath
	 */
	public String getTargetPdfpath() {
		return targetPdfpath;
	}

	/**
	 * @param targetPdfpath
	 *            the targetPdfpath toset
	 */
	public void setTargetPdfpath(String targetPdfpath) {
		this.targetPdfpath = targetPdfpath;
	}

	/**
	 * @return the ticket
	 */
	public Advisory getAdvisory() {
		return advisory;
	}

	/**
	 * @param ticket
	 *            the ticket to set
	 */
	public void setAdvisory(Advisory advisory) {
		this.advisory = advisory;
	}

} 

   

    測試類:TestPdf

/**
 * 
 */
package org.csun.nc.utils;

import java.io.File;
import java.io.IOException;

import org.csun.nc.pojo.Advisory;

/**
 * @author liqian
 * @date   2018年3月26日
 * @time   上午11:26:23
 * Description : 
 */
public class TestPdf {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
            
			Advisory advisory = new Advisory();  
            advisory.setAddress("1000.0");  
            advisory.setAdvisoryName("1000.0");  
            advisory.setAdvisoryPhone("2000.0"); 
            advisory.setOldName("2000.0"); 
            advisory.setReceptionist("100.00");
             
            PDFTempletTicket pdfTT = new PDFTempletTicket();  
             
            pdfTT.setTemplatePdfPath("D:\\aa.pdf");  
            pdfTT.setTargetPdfpath("D:\\33.pdf"); 
            pdfTT.setAdvisory(advisory); 
             
            File file = new File("D:\\33.pdf");
            file.createNewFile();  
            pdfTT.templetTicket(file);  
             
             
  }  

}

    實體類:Advisory

package org.csun.nc.pojo;

import java.io.Serializable;
import java.util.Date;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.csun.nc.valid.AdvisoryValid;
import org.csun.nc.valid.CheckInValid;
import org.hibernate.validator.constraints.NotEmpty;

public class Advisory implements Serializable {
	
	@NotNull(message = "諮詢人advisoryId爲空",groups = {AdvisoryValid.class})
    private Integer advisoryId;  //諮詢id
	
    private String advisoryName; //諮詢人姓名

    @NotEmpty(message = "諮詢人電話不能爲空",groups = {AdvisoryValid.class})
    @Pattern(regexp = "^[1][3,4,5,7,8][0-9]{9}$",message = "諮詢人電話格式不正確",groups = {AdvisoryValid.class})
    private String advisoryPhone; //諮詢人電話
    
    @NotEmpty(message = "老人姓名不能爲空",groups = {AdvisoryValid.class})
    @Size(min=1,max=20,message = "老人姓名長度最小爲1,最大爲20",groups = {AdvisoryValid.class})
    
    private String oldName;      //老人姓名

    private Integer advisoryWay; //諮詢方式

    private String receptionist; //接待人

    private String address;      //地址

    private String advisoryDate; //諮詢時間

    private Integer valid;       //該條記錄是否有效    1-有效  0-無效

    private Date createTime;	//創建時間

    private Date modifyTime;	//修改時間

    private Integer modifier;	//修改人 (默認爲管理員操作)

    private static final long serialVersionUID = 1L;

    public Integer getAdvisoryId() {
        return advisoryId;
    }

    public void setAdvisoryId(Integer advisoryId) {
        this.advisoryId = advisoryId;
    }

    public String getAdvisoryName() {
        return advisoryName;
    }

    public void setAdvisoryName(String advisoryName) {
        this.advisoryName = advisoryName;
    }

    public String getAdvisoryPhone() {
        return advisoryPhone;
    }

    public void setAdvisoryPhone(String advisoryPhone) {
        this.advisoryPhone = advisoryPhone;
    }

    public String getOldName() {
        return oldName;
    }

    public void setOldName(String oldName) {
        this.oldName = oldName;
    }

    public Integer getAdvisoryWay() {
        return advisoryWay;
    }

    public void setAdvisoryWay(Integer advisoryWay) {
        this.advisoryWay = advisoryWay;
    }

    public String getReceptionist() {
        return receptionist;
    }

    public void setReceptionist(String receptionist) {
        this.receptionist = receptionist;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAdvisoryDate() {
        return advisoryDate;
    }

    public void setAdvisoryDate(String advisoryDate) {
        this.advisoryDate = advisoryDate;
    }

    public Integer getValid() {
        return valid;
    }

    public void setValid(Integer valid) {
        this.valid = valid;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public Integer getModifier() {
        return modifier;
    }

    public void setModifier(Integer modifier) {
        this.modifier = modifier;
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        Advisory other = (Advisory) that;
        return (this.getAdvisoryId() == null ? other.getAdvisoryId() == null : this.getAdvisoryId().equals(other.getAdvisoryId()))
            && (this.getAdvisoryName() == null ? other.getAdvisoryName() == null : this.getAdvisoryName().equals(other.getAdvisoryName()))
            && (this.getAdvisoryPhone() == null ? other.getAdvisoryPhone() == null : this.getAdvisoryPhone().equals(other.getAdvisoryPhone()))
            && (this.getOldName() == null ? other.getOldName() == null : this.getOldName().equals(other.getOldName()))
            && (this.getAdvisoryWay() == null ? other.getAdvisoryWay() == null : this.getAdvisoryWay().equals(other.getAdvisoryWay()))
            && (this.getReceptionist() == null ? other.getReceptionist() == null : this.getReceptionist().equals(other.getReceptionist()))
            && (this.getAddress() == null ? other.getAddress() == null : this.getAddress().equals(other.getAddress()))
            && (this.getAdvisoryDate() == null ? other.getAdvisoryDate() == null : this.getAdvisoryDate().equals(other.getAdvisoryDate()))
            && (this.getValid() == null ? other.getValid() == null : this.getValid().equals(other.getValid()))
            && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
            && (this.getModifyTime() == null ? other.getModifyTime() == null : this.getModifyTime().equals(other.getModifyTime()))
            && (this.getModifier() == null ? other.getModifier() == null : this.getModifier().equals(other.getModifier()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getAdvisoryId() == null) ? 0 : getAdvisoryId().hashCode());
        result = prime * result + ((getAdvisoryName() == null) ? 0 : getAdvisoryName().hashCode());
        result = prime * result + ((getAdvisoryPhone() == null) ? 0 : getAdvisoryPhone().hashCode());
        result = prime * result + ((getOldName() == null) ? 0 : getOldName().hashCode());
        result = prime * result + ((getAdvisoryWay() == null) ? 0 : getAdvisoryWay().hashCode());
        result = prime * result + ((getReceptionist() == null) ? 0 : getReceptionist().hashCode());
        result = prime * result + ((getAddress() == null) ? 0 : getAddress().hashCode());
        result = prime * result + ((getAdvisoryDate() == null) ? 0 : getAdvisoryDate().hashCode());
        result = prime * result + ((getValid() == null) ? 0 : getValid().hashCode());
        result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
        result = prime * result + ((getModifyTime() == null) ? 0 : getModifyTime().hashCode());
        result = prime * result + ((getModifier() == null) ? 0 : getModifier().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", advisoryId=").append(advisoryId);
        sb.append(", advisoryName=").append(advisoryName);
        sb.append(", advisoryPhone=").append(advisoryPhone);
        sb.append(", oldName=").append(oldName);
        sb.append(", advisoryWay=").append(advisoryWay);
        sb.append(", receptionist=").append(receptionist);
        sb.append(", address=").append(address);
        sb.append(", advisoryDate=").append(advisoryDate);
        sb.append(", valid=").append(valid);
        sb.append(", createTime=").append(createTime);
        sb.append(", modifyTime=").append(modifyTime);
        sb.append(", modifier=").append(modifier);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

會遇到的問題:走到如下圖的位置可能會報“pdf not found as file or resource.”找不到文件的錯誤;

解決方法:https://blog.csdn.net/jla_jobs/article/details/30074537 這裏面講的很仔細;

我的解決方法:用下面的代碼替換如下圖標出的那一行運行成功後,再刪除下面的代碼,還原代碼再運行就正常了。

                        原因我看別人說的是內存溢出(但是表示不懂)

FileInputStream file = new FileInputStream(inputFile);//創建流文件,其中inputFile是你的PDF文檔路徑
     byte[] b=  new byte[file.available()];//獲取字節
    if(file.read(b)>0){
     file.read(b,0,b.length);//將字節讀入到文件裏
    }
 
  PdfReader reader = null;// iText一個類

    //這前直接將文件路徑放進來,結果就是讀大PDF文件(我的是大於1M)時讀不到內存裏。
   //reader = new PdfReader(inputFile);

    //解決問題的關鍵

    / /更換成這種方式,就可以讀到大文件了。我的1M的文件是讀到了,再大的沒有測試過。
    reader = new PdfReader(new PdfReader(b)); 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章