为了账号安全,请及时绑定邮箱和手机立即绑定

使用速度模板生成的 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 。

//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 关注
  • 148 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号