告诉 Flying Saucer 或 BIRT 缩放 html 以适合单个 pdf 页面

发布于 2024-12-04 18:31:04 字数 674 浏览 1 评论 0原文

我的应用程序生成 HTML 报告。用户希望报告适合单个 PDF 页面 - 无论您需要多大的缩放比例。

目前,他们在 Internet Explorer 8 中渲染页面,并在“打印预览”窗口中手动定义自定义缩放级别,然后将页面打印为 PDF。我需要自动执行此操作。如何实现这一目标?

这些报告实际上是由 BIRT 创建的,因此基于 BIRT 的解决方案也很好。使用 BIRT 缩放选项可缩放文档以适合高度,但不适合宽度(因为它首先呈现 HTML,然后应用缩放)。

现在我尝试使用 Flying Saucer 将 BIRT 生成的 HTML 转换为 PDF。

我见过与 abcPDF (.NET) 和 CF8(见下文)相关的类似问题,但我需要一个基于 Java 的解决方案,并且我无法在 Flying Saucer 或 BIRT 中使用相同的技术。

告诉 abcPdf 缩放 html以适应单个 pdf 页面

将 PDF 缩放为单个页面页面

My application generates HTML reports. The users want the report to fit a single PDF page - doesn't matter how much zoom you need for it.

Currently they render the page in Internet Explorer 8, and manually define a custom zoom level at the Print Preview window, then they print the page to PDF. I need to perform this operation automatically. How to achieve this?

The reports are actually created by BIRT, so BIRT-based solution is fine, too. Using the BIRT zoom options scale the document to fit height, but it doesn't fit width (because it first renders the HTML then apply the zoom).

Now I'm trying to use Flying Saucer to convert to PDF the BIRT-generated HTML.

I've saw similar questions related to abcPDF (.NET) and CF8 (se below) but I need a Java-based solution, and I wasn't able to use the same techniques with Flying Saucer or BIRT.

Tell abcPdf to scale the html to fit on a single pdf page

scale PDF to single page

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩耳倾听 2024-12-11 18:31:04

我们向 Flying Saucer 项目添加了一个补丁请求请求,以允许 "itextrenderer 适合页面宽度"< /a>.

提议的补丁的想法是,我们将使页面尽可能宽(以避免截断),然后您可以在打印时使用 PDF 阅读器来适应页面。

We have added a pull request to Flying Saucer project for a patch to allow "itextrenderer fitting to page width".

The idea of the proposed patch is that we will make the page as wide as necessary (to avoid truncation) and then you can use PDF reader to fit to page when printing.

感性 2024-12-11 18:31:04

这是我基于 Flying Saucer / iText 找到的解决方案:

// A4 paper size = 297x210 mm
int PAGE_WIDTH = 2100;
int PAGE_LENGTH = 2970;
float MARGIN = 19.05f; // 0.75 inch

int width = PAGE_WIDTH; 

// Java2DRenderer.NO_HEIGHT = -1 (it's private)
// if used the required height is computed
Java2DRenderer renderer = new Java2DRenderer(dom, width, -1); 
BufferedImage img = renderer.getImage();

// Adjusts width based on required height
int height = img.getHeight(); 
width = (int) Math.round(height * PAGE_WIDTH / PAGE_LENGTH);

// Render same document again, now with the right dimensions
renderer = new Java2DRenderer(dom, width, height);
img = renderer.getImage();

com.lowagie.text.Document pdf = new com.lowagie.text.Document(PageSize.A4);
pdf.setMargins(MARGIN,MARGIN,MARGIN,MARGIN); 
PdfWriter.getInstance(pdf, new FileOutputStream(args[0]+".pdf"));
pdf.open();
com.lowagie.text.Image pdfImage = com.lowagie.text.Image.getInstance(img,Color.WHITE);
Rectangle ps = pdf.getPageSize();
pdfImage.scaleAbsolute(
    ps.getWidth()-pdf.leftMargin()-pdf.rightMargin(),
    ps.getHeight()-pdf.topMargin()-pdf.bottomMargin()
);
pdf.add(pdfImage);
pdf.close();

That's the solution I found, based on Flying Saucer / iText:

// A4 paper size = 297x210 mm
int PAGE_WIDTH = 2100;
int PAGE_LENGTH = 2970;
float MARGIN = 19.05f; // 0.75 inch

int width = PAGE_WIDTH; 

// Java2DRenderer.NO_HEIGHT = -1 (it's private)
// if used the required height is computed
Java2DRenderer renderer = new Java2DRenderer(dom, width, -1); 
BufferedImage img = renderer.getImage();

// Adjusts width based on required height
int height = img.getHeight(); 
width = (int) Math.round(height * PAGE_WIDTH / PAGE_LENGTH);

// Render same document again, now with the right dimensions
renderer = new Java2DRenderer(dom, width, height);
img = renderer.getImage();

com.lowagie.text.Document pdf = new com.lowagie.text.Document(PageSize.A4);
pdf.setMargins(MARGIN,MARGIN,MARGIN,MARGIN); 
PdfWriter.getInstance(pdf, new FileOutputStream(args[0]+".pdf"));
pdf.open();
com.lowagie.text.Image pdfImage = com.lowagie.text.Image.getInstance(img,Color.WHITE);
Rectangle ps = pdf.getPageSize();
pdfImage.scaleAbsolute(
    ps.getWidth()-pdf.leftMargin()-pdf.rightMargin(),
    ps.getHeight()-pdf.topMargin()-pdf.bottomMargin()
);
pdf.add(pdfImage);
pdf.close();
无人问我粥可暖 2024-12-11 18:31:04

首先,我没有使用过飞碟。

但是,也许其他人可能正在阅读您的问题并需要不同的解决方案。

如果您使用 Java BIRT 引擎并自行设置渲染选项,则可以应用:

[PAGE_OVERFLOW = FIT_TO_PAGE_SIZE]

First, I have not used Flying Saucer.

However, perhaps others may be reading your question and need a different solution.

If you are using the Java BIRT Engine and setting the render options yourself, you can apply:

[PAGE_OVERFLOW = FIT_TO_PAGE_SIZE]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文