瀏覽器列印iText pdf實現方式

2021-06-29 16:36:09 字數 2079 閱讀 1289

首先需要把瀏覽器的外掛程式配置成功,具體步驟請看:

使用itext實現pdf的生成:匯入itext-pdfa-5.5.5.jar 和itext-xtra-5.5.5.jar 和itextpdf-5.5.5.jar

一共5步生成pdf:1、建立文件:document document = new document(pagesize.a4.rotate());//設定a4紙大小,還可加4個引數分別為left,right,top,bottom,可自定義紙張大小,引數換為new rectangle(寬f,高f),單位為畫素。

2、建立乙個pdfwriter例項:pdfwriter.getinstance(document, new fileoutputstream(pathkit.getwebrootpath() + "/temp/printstorage.pdf"));

3、開啟document:document.open();

4.加入內容,加入內容之前需要先設定一下字型,否則pdf不能列印中文的。之前考慮加itextasian.jar但由於itext版本高所以jar包路徑不對,手動換路徑頁不行,就考慮以下方法:

basefont basefont = basefont.createfont("c:/windows/fonts/simsun.ttc,1", basefont.identity_h, basefont.embedded);

font font = new font(basefont, 12, font.normal);

//title

paragraph paragraph=new paragraph("入庫單",font);

paragraph.setalignment(1);

string date=stolist.get(0).getdate("stsu_data").tostring().split("-");

paragraph p=new paragraph(date[0]+"年"+date[1]+"月"+date[2]+"日          "+"入庫單號:"+stsu.getstr("stsu_code")+"            單位:冊",font);//按照入庫日期

p.setalignment(1);

document.add(paragraph);

document.add(p);

//table start

pdfptable table = new pdfptable(new float);//table 設定寬度

table.setspacingbefore(5);

//head

pdfpcell c1 = new pdfpcell(new phrase("序號", font));

table.addcell(c1).sethorizontalalignment(1);

pdfpcell c2 = new pdfpcell(new phrase("入庫品種版本", font));

table.addcell(c2).sethorizontalalignment(1);

pdfpcell c3 = new pdfpcell(new phrase("期數", font));

table.addcell(c3).sethorizontalalignment(1);

pdfpcell c5 = new pdfpcell(new phrase("數量", font));

table.addcell(c5).sethorizontalalignment(1);

pdfpcell c6 = new pdfpcell(new phrase("備註", font));

table.addcell(c6).sethorizontalalignment(1);

//value

for(int i=0;i5、關閉文件:document.close();

跳轉到頁面路徑:

redirect(getrequest().getscheme() + "://" +getrequest().getservername() + ":" +getrequest().getserverport() + getrequest().getcontextpath()+"/temp/printstorage.pdf");

瀏覽器列印

2004年12月21日 author longware longware 126.usage note 瀏覽器需求ie6.0 在http頁面上列印必須將ie的安全級別降低.document.writeln document.writeln document.write document.writel...

JS實現瀏覽器列印 列印預覽

目前正在做瀏覽器端採用js方式實現列印這麼乙個功能,js列印實現的方法很多,但是相容各個瀏覽器實現列印預覽的功能有些棘手,現將實現的內容及遇到的問題記錄下來,希望有大牛看到所提的問題後可以給予解答,在此感謝啦。window.print 會彈出列印對話方塊,列印的是window.document.bo...

JS實現瀏覽器列印 列印預覽示例

1.js實現列印的方式 方式一 window.print window.print 會彈出列印對話方塊,列印的是window.document.body.innerhtml中的內容,下面是從網上摘到的乙個區域性列印的例子,該例子的不足是列印會修改頁面的內容。1不需要列印的地方 2這裡不要列印啊 列印...