ssm+maven实现pdf导出

第一步:根据需求新建一个word文档

第二步:下载Adobe Acrobat DC并安装

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

ssm+maven实现pdf导出_第1张图片ssm+maven实现pdf导出_第2张图片

ssm+maven实现pdf导出_第3张图片

第四步:创建form域。a.当word文档以表格形式就不需要再自己额外添加域了;b.当word文档没有表格,需要自己添加域,如下图进行添加域。(这里的域指的是需要被替换的位置)

ssm+maven实现pdf导出_第4张图片

ssm+maven实现pdf导出_第5张图片

第五步:pom.xml中加入


		
        
        com.itextpdf
            itext-asian
            5.2.0
        

    工具类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)); 

ssm+maven实现pdf导出_第6张图片


你可能感兴趣的:(ssm,maven,pdf导出,pdf导出)