亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用速度模板生成的 pdf 中的書簽

使用速度模板生成的 pdf 中的書簽

慕哥9229398 2022-12-21 12:57:08
我有大量數據,這些數據將用于使用 velocity 模板生成 PDF。我有使用 .vm 文件生成的索引頁,它是一個表。我應該提供從索引頁面到其他頁面的書簽。我嘗試在 HTML 中只使用 href。索引.vm:<table><tr><td>1</td><td><a href="#go">chapter1</a><td></tr></table>程序集.vm:<table><tr><p1 id="go">assembly1</p></tr></table>預計在索引頁中有鏈接,單擊它會轉到相應的內容頁。
查看完整描述

2 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

使用 itext 和 .vm 文件生成 pdf 后,在生成時將帶有頁碼的描述存儲在地圖中,這是使用以下代碼實現的


HashMap<String, Object> map = new HashMap<String, Object>();

    map.put("Title", "INDEX");

    map.put("Action", "GoTo");

    map.put("Page", String.format("%d Fit", 8));

    ArrayList<HashMap<String, Object>> kids = new ArrayList<HashMap<String,Object>>();


    for(BookMark book : BookMarks) {

        HashMap<String, Object> kid = new HashMap<String, Object>();

        kid.put("Title", book.getDescription());

        kid.put("Action", "GoTo");

        kid.put("Page", String.format("%d Fit", book.getPageNumber()));


        ArrayList<HashMap<String, Object>> leafs = new ArrayList<HashMap<String,Object>>();

        for(BookMark books : book.getLeaf()) {

            HashMap<String, Object> leaf = new HashMap<String, Object>();

            leaf.put("Title", books.getDescription());

            leaf.put("Action", "GoTo");

            leaf.put("Page", String.format("%d Fit", books.getPageNumber()));

            leafs.add(leaf);

        }

        kid.put("Kids", leafs);

        kids.add(kid);

    }


    map.put("Kids", kids);

    ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String,Object>>();

    outlines.add(map);

    PdfReader reader = new PdfReader(env.getProperty("path.generated.pdf").concat(fileName));

    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(env.getProperty("path.generated.pdf").concat(catalogInfo.getCatalogName().trim().concat("raw1")).concat(".pdf")));

    stamper.setOutlines(outlines);

    stamper.setFullCompression();

    stamper.close();

    reader.close();

    File file = new File(env.getProperty("path.generated.pdf").concat(fileName)); 

    file.delete();


查看完整回答
反對 回復 2022-12-21
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

我在從模板生成 PDF 時遇到了同樣的問題,但我使用的是 JSP。每個模板引擎都是相同的邏輯。

要實現它,請在您自己的服務器上請求從 HTML 模板中獲取生成的內容,并使用flying-saucer將其轉換為 PDF 。

http://img1.sycdn.imooc.com//63a2924b0001afc206550389.jpg

所以基本上你會有


根據參數返回生成的 Velocity 模板的 servlet


(即:http: //127.0.0.1/getgeneratedpdf)


    dopost etc. ...

另一個 servlet 使用所需的參數調用第一個 servlet 以獲取 HTML 中生成的內容


   URLConnection connection = new URL(urlOfTheServletAbove).openConnection();

   connection.setDoOutput(true); // POST

   connection.setRequestProperty("Accept-Charset", "UTF-8");

   connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

   try (OutputStream output = connection.getOutputStream()) {

       // parameters is encoded query string

       output.write(parameters.getBytes(StandardCharsets.UTF_8));

   }

   BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

   StringBuilder sb = new StringBuilder();

   String inputLine;

   while ((inputLine = in.readLine()) != null) { sb.append(inputLine); }

   in.close();

基于飛碟的 PDF 生成器


import org.xhtmlrenderer.pdf.ITextRenderer;

// ...

private static final String TMP_DIR = System.getProperty("java.io.tmpdir");

// ...

File tempPdf = new File(TMP_DIR+tempPdfName);

if (!tempPdf.exists()) { tempPdf.createNewFile(); }

FileOutputStream fos = new FileOutputStream(tempPdf);

new ITextRenderer() {{

    setDocumentFromString(sb.toString());

    layout();

    createPDF(fos);

}};

fos.close();

// ...

然后將PDF寫入response


void writePDFContentToResponse(File pdf, HttpServletResponse response) throws IOException {

    InputStream fis = new FileInputStream(pdf);

    String mimeType = getServlet().getServletContext().getMimeType(pdf.getAbsolutePath());

    response.setContentType(mimeType != null ? mimeType : "application/octet-stream");

    response.setContentLength((int) pdf.length());

    response.setHeader("Content-Disposition", "attachment; filename=yourPDFName.pdf"); // or +pdf.getName();

    ServletOutputStream os = response.getOutputStream();

    byte[] bufferData = new byte[1024];

    int read = 0;

    while((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); }

    os.flush();

    os.close();

    fis.close();

    response.flushBuffer();

    Files.delete(pdf.toPath());

}


查看完整回答
反對 回復 2022-12-21
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號