使用速度模板生成的 pdf 中的书签

我有大量数据,这些数据将用于使用 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>

预计在索引页中有链接,单击它会转到相应的内容页。


慕哥9229398
浏览 244回答 2
2回答

GCT1015

使用 itext 和 .vm 文件生成 pdf 后,在生成时将带有页码的描述存储在地图中,这是使用以下代码实现的HashMap<String, Object> map = new HashMap<String, Object>();&nbsp; &nbsp; map.put("Title", "INDEX");&nbsp; &nbsp; map.put("Action", "GoTo");&nbsp; &nbsp; map.put("Page", String.format("%d Fit", 8));&nbsp; &nbsp; ArrayList<HashMap<String, Object>> kids = new ArrayList<HashMap<String,Object>>();&nbsp; &nbsp; for(BookMark book : BookMarks) {&nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, Object> kid = new HashMap<String, Object>();&nbsp; &nbsp; &nbsp; &nbsp; kid.put("Title", book.getDescription());&nbsp; &nbsp; &nbsp; &nbsp; kid.put("Action", "GoTo");&nbsp; &nbsp; &nbsp; &nbsp; kid.put("Page", String.format("%d Fit", book.getPageNumber()));&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<HashMap<String, Object>> leafs = new ArrayList<HashMap<String,Object>>();&nbsp; &nbsp; &nbsp; &nbsp; for(BookMark books : book.getLeaf()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, Object> leaf = new HashMap<String, Object>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leaf.put("Title", books.getDescription());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leaf.put("Action", "GoTo");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leaf.put("Page", String.format("%d Fit", books.getPageNumber()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leafs.add(leaf);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; kid.put("Kids", leafs);&nbsp; &nbsp; &nbsp; &nbsp; kids.add(kid);&nbsp; &nbsp; }&nbsp; &nbsp; map.put("Kids", kids);&nbsp; &nbsp; ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String,Object>>();&nbsp; &nbsp; outlines.add(map);&nbsp; &nbsp; PdfReader reader = new PdfReader(env.getProperty("path.generated.pdf").concat(fileName));&nbsp; &nbsp; PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(env.getProperty("path.generated.pdf").concat(catalogInfo.getCatalogName().trim().concat("raw1")).concat(".pdf")));&nbsp; &nbsp; stamper.setOutlines(outlines);&nbsp; &nbsp; stamper.setFullCompression();&nbsp; &nbsp; stamper.close();&nbsp; &nbsp; reader.close();&nbsp; &nbsp; File file = new File(env.getProperty("path.generated.pdf").concat(fileName));&nbsp;&nbsp; &nbsp; file.delete();

慕运维8079593

我在从模板生成 PDF 时遇到了同样的问题,但我使用的是 JSP。每个模板引擎都是相同的逻辑。要实现它,请在您自己的服务器上请求从 HTML 模板中获取生成的内容,并使用flying-saucer将其转换为 PDF 。所以基本上你会有根据参数返回生成的 Velocity 模板的 servlet(即:http: //127.0.0.1/getgeneratedpdf)&nbsp; &nbsp; dopost etc. ...另一个 servlet 使用所需的参数调用第一个 servlet 以获取 HTML 中生成的内容&nbsp; &nbsp;URLConnection connection = new URL(urlOfTheServletAbove).openConnection();&nbsp; &nbsp;connection.setDoOutput(true); // POST&nbsp; &nbsp;connection.setRequestProperty("Accept-Charset", "UTF-8");&nbsp; &nbsp;connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");&nbsp; &nbsp;try (OutputStream output = connection.getOutputStream()) {&nbsp; &nbsp; &nbsp; &nbsp;// parameters is encoded query string&nbsp; &nbsp; &nbsp; &nbsp;output.write(parameters.getBytes(StandardCharsets.UTF_8));&nbsp; &nbsp;}&nbsp; &nbsp;BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));&nbsp; &nbsp;StringBuilder sb = new StringBuilder();&nbsp; &nbsp;String inputLine;&nbsp; &nbsp;while ((inputLine = in.readLine()) != null) { sb.append(inputLine); }&nbsp; &nbsp;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() {{&nbsp; &nbsp; setDocumentFromString(sb.toString());&nbsp; &nbsp; layout();&nbsp; &nbsp; createPDF(fos);}};fos.close();// ...然后将PDF写入responsevoid writePDFContentToResponse(File pdf, HttpServletResponse response) throws IOException {&nbsp; &nbsp; InputStream fis = new FileInputStream(pdf);&nbsp; &nbsp; String mimeType = getServlet().getServletContext().getMimeType(pdf.getAbsolutePath());&nbsp; &nbsp; response.setContentType(mimeType != null ? mimeType : "application/octet-stream");&nbsp; &nbsp; response.setContentLength((int) pdf.length());&nbsp; &nbsp; response.setHeader("Content-Disposition", "attachment; filename=yourPDFName.pdf"); // or +pdf.getName();&nbsp; &nbsp; ServletOutputStream os = response.getOutputStream();&nbsp; &nbsp; byte[] bufferData = new byte[1024];&nbsp; &nbsp; int read = 0;&nbsp; &nbsp; while((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); }&nbsp; &nbsp; os.flush();&nbsp; &nbsp; os.close();&nbsp; &nbsp; fis.close();&nbsp; &nbsp; response.flushBuffer();&nbsp; &nbsp; Files.delete(pdf.toPath());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java