1. 程式人生 > >java端使用IText下載PDF.(包含JAR包)

java端使用IText下載PDF.(包含JAR包)

java後臺生成PDF傳給前臺介面,前臺介面生成下載連結IText,
所用的jar包百度雲資源(itextpdf-5.5.8,itext-asian)
不會發生什麼配置檔案丟失的jar包,我也是翻了好久才找到。
連結:https://pan.baidu.com/s/1rSp7D4aWBvx25vmv2mQWNg 密碼:hfcj

1.原始碼,我自己測試過的。主要生成PDF中表格資料的地方,若是圖片等其他資訊,等待有需求時,進一步實現。
直接JAVA指定路徑實現。

public class ExportPdfUtil {
private Font FontChinese;

public void simplePDF(String deviceType) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    try {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        FontChinese = new Font(bfChinese, 12, Font.NORMAL);
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("E:\\FIRSTPDF.pdf"));

// PdfWriter.getInstance(document, baos);
document.open();
PdfPTable table = null;
if (deviceType == “固話”) {
table = new PdfPTable(6);
table.addCell(getCell(“清單型別”, 1, 1));
table.addCell(getCell(“使用裝置”, 1, 1));
table.addCell(getCell(“日期”, 1, 1));
table.addCell(getCell(“起始時間”, 1, 1));
table.addCell(getCell(“通話時長”, 1, 1));
table.addCell(getCell(“通訊費用”, 1, 1));

            table.addCell(getCell("固網彩鈴愛音樂鈴聲費", 1, 1));
            table.addCell(getCell("2162146506", 1, 1));
            table.addCell(getCell("20180801", 1, 1));
            table.addCell(getCell("00:00:00", 1, 1));
            table.addCell(getCell("0", 1, 1));
            table.addCell(getCell("¥0.00", 1, 1));
        } else if (deviceType == "手機") {
            table = new PdfPTable(10);
            table.addCell(getCell("通話型別", 1, 1));
            table.addCell(getCell("開始時間", 1, 1));
            table.addCell(getCell("時長", 1, 1));
            table.addCell(getCell("呼叫型別", 1, 1));
            table.addCell(getCell("對方號碼", 1, 1));
            table.addCell(getCell("通話地點", 1, 1));
            table.addCell(getCell("基本費用(元)", 1, 1));
            table.addCell(getCell("資訊費用(元)", 1, 1));
            table.addCell(getCell("其他費用(元)", 1, 1));
            table.addCell(getCell("總費用(元)", 1, 1));

            table.addCell(getCell("固網彩鈴愛音樂鈴聲費", 1, 1));
            table.addCell(getCell("2162146506", 1, 1));
            table.addCell(getCell("20180801", 1, 1));
            table.addCell(getCell("00:00:00", 1, 1));
            table.addCell(getCell("0", 1, 1));
            table.addCell(getCell("¥0.00", 1, 1));
            table.addCell(getCell("20180801", 1, 1));
            table.addCell(getCell("00:00:00", 1, 1));
            table.addCell(getCell("0", 1, 1));
            table.addCell(getCell("¥0.00", 1, 1));
        }
        table.setWidthPercentage(110); // 設定表格寬度

        document.add(table);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
    PdfPCell cell = new PdfPCell();
    try {
        cell = new PdfPCell(new Phrase(cellValue, FontChinese));
        cell.setRowspan(rowSpan);
        cell.setColspan(colspan);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cell;
}

public static void main(String[] args) {
    ExportPdfUtil c = new ExportPdfUtil();
    c.simplePDF("手機");
}

}

2.java配合瀏覽器實現PDF檔案下載
以下是將java的PDF檔案傳輸給瀏覽器下載。配合任何一種controller都可以,我這裡用的是SpringBoot的controller.
跟上面的程式碼非常相似。就是加入了流,將後臺的指定路徑下載,轉交給瀏覽器指定路徑去下載。

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;

import org.springframework.stereotype.Service;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
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.itextpdf.text.pdf.PdfStructTreeController.returnType;

@Service(“exportPdfUtil”)
public class ExportPdfUtil {
private Font FontChinese;

public ByteArrayOutputStream simplePDF(String deviceType) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    try {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        FontChinese = new Font(bfChinese, 12, Font.NORMAL);
        Document document = new Document();
        PdfWriter.getInstance(document, baos);
        document.open();
        PdfPTable table = null; 
        if (deviceType == "固話") {
            table = new PdfPTable(6);//設定一共多少列表的
            table.addCell(getCell("清單型別", 1, 1));
            table.addCell(getCell("使用裝置", 1, 1));
            table.addCell(getCell("日期", 1, 1));
            table.addCell(getCell("起始時間", 1, 1));
            table.addCell(getCell("通話時長", 1, 1));
            table.addCell(getCell("通訊費用", 1, 1));

            table.addCell(getCell("固網彩鈴愛音樂鈴聲費", 1, 1));
            table.addCell(getCell("2162146506", 1, 1));
            table.addCell(getCell("20180801", 1, 1));
            table.addCell(getCell("00:00:00", 1, 1));
            table.addCell(getCell("0", 1, 1));
            table.addCell(getCell("¥0.00", 1, 1));
        } else if (deviceType == "手機") {
            table = new PdfPTable(10);
            table.addCell(getCell("通話型別", 1, 1));
            table.addCell(getCell("開始時間", 1, 1));
            table.addCell(getCell("時長", 1, 1));
            table.addCell(getCell("呼叫型別", 1, 1));
            table.addCell(getCell("對方號碼", 1, 1));
            table.addCell(getCell("通話地點", 1, 1));
            table.addCell(getCell("基本費用(元)", 1, 1));
            table.addCell(getCell("資訊費用(元)", 1, 1));
            table.addCell(getCell("其他費用(元)", 1, 1));
            table.addCell(getCell("總費用(元)", 1, 1));

            table.addCell(getCell("固網彩鈴愛音樂鈴聲費", 1, 1));
            table.addCell(getCell("2162146506", 1, 1));
            table.addCell(getCell("20180801", 1, 1));
            table.addCell(getCell("00:00:00", 1, 1));
            table.addCell(getCell("0", 1, 1));
            table.addCell(getCell("¥0.00", 1, 1));
            table.addCell(getCell("20180801", 1, 1));
            table.addCell(getCell("00:00:00", 1, 1));
            table.addCell(getCell("0", 1, 1));
            table.addCell(getCell("¥0.00", 1, 1));
        }
        table.setWidthPercentage(110); // 設定表格寬度

        document.add(table);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return baos;
}

private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
    PdfPCell cell = new PdfPCell();
    try {
        cell = new PdfPCell(new Phrase(cellValue, FontChinese));
        cell.setRowspan(rowSpan);
        cell.setColspan(colspan);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cell;
}

/* public static void main(String[] args) {
ExportPdfUtil c = new ExportPdfUtil();
c.simplePDF(“手機”);
}*/
}

上面是util,下面是controller中的方法。。
//下載PDF
@RequestMapping(“/downloadPdf”)
public void downloadPdf(HttpServletRequest request,HttpServletResponse response,Model model){
try {
ByteArrayOutputStream baos=exportPdfUtil.simplePDF(“手機”);
//設定請求返回型別
response.setContentType(“application/pdf”);
response.setHeader(“Content-Disposition”, “attachment; filename=file.pdf”);
response.setContentLength(baos.size());
OutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}

這樣,前臺使用window.open()方式,訪問controller,即可實現pdf下載,其中具體資料,根據你們自己業務的情況自行發揮。