You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过iText HtmlConverter动态设置PDF页面横竖方向?

问题描述

我们使用iText的HtmlConverter将HTML转换为PDF,代码如下:

public static void generate(HttpMethod httpMethod, String formName,
                           String requestSource,
                           MultiValueMap<String, String> parameters) {

        UriComponentsBuilder uriComponentsBuilder = 
        UriComponentsBuilder.fromHttpUrl(htmlFormProperties.getBaseUrl());
        uriComponentsBuilder.queryParam("formName", formName);
        uriComponentsBuilder.queryParam("requestSource", requestSource);
        String htmlForm = htmlFormClientRestTemplate.postForObject(uriComponentsBuilder.build().toUriString(), parameters, String.class);
        ByteArrayOutputStream byteArrayOutputStream = new 
        ByteArrayOutputStream(html.getBytes(Charset.defaultCharset()).length);
        HtmlConverter.convertToPdf(html, byteArrayOutputStream);
    }

目前尝试将页面方向改为横向(landscape)但未成功,想知道如何实现页面方向(纵向/横向)的动态切换,恳请提供思路或建议。

实现动态切换页面方向的思路与代码示例

要实现iText HtmlConverter转换PDF时动态切换页面方向,核心是通过配置转换参数或HTML内联CSS来控制,以下是两种可行方案:

方案一:通过ConverterProperties配置页面方向

通过ConverterProperties设置自定义的MediaDeviceDescription,可以直接指定PDF的页面尺寸(横向尺寸可通过PageSize.A4.rotate()获取),实现动态切换。

修改后的代码示例:

public static void generate(HttpMethod httpMethod, String formName,
                           String requestSource,
                           MultiValueMap<String, String> parameters,
                           boolean isLandscape) { // 新增参数控制页面方向

        UriComponentsBuilder uriComponentsBuilder = 
        UriComponentsBuilder.fromHttpUrl(htmlFormProperties.getBaseUrl());
        uriComponentsBuilder.queryParam("formName", formName);
        uriComponentsBuilder.queryParam("requestSource", requestSource);
        String htmlForm = htmlFormClientRestTemplate.postForObject(uriComponentsBuilder.build().toUriString(), parameters, String.class);
        ByteArrayOutputStream byteArrayOutputStream = new 
        ByteArrayOutputStream(htmlForm.getBytes(Charset.defaultCharset()).length);

        // 构建转换配置
        ConverterProperties converterProps = new ConverterProperties();
        MediaDeviceDescription mediaDesc = new MediaDeviceDescription(MediaType.SCREEN);
        // 根据传入参数选择页面尺寸
        PageSize targetSize = isLandscape ? PageSize.A4.rotate() : PageSize.A4;
        mediaDesc.setPageSize(targetSize);
        converterProps.setMediaDeviceDescription(mediaDesc);

        // 传入配置执行转换
        HtmlConverter.convertToPdf(htmlForm, byteArrayOutputStream, converterProps);
    }

方案二:通过HTML内联CSS控制页面方向

如果允许修改生成的HTML内容,可以在HTML的<head>中动态插入打印媒体查询的CSS规则,iText会解析并应用该规则:

<head>
    <style>
        @media print {
            @page {
                size: A4 landscape; /* 横向模式 */
                /* 纵向模式改为 size: A4 portrait; */
            }
        }
    </style>
</head>

你可以根据业务逻辑,在生成htmlForm字符串时动态替换或插入对应的CSS规则,实现页面方向的动态切换。

关键注意点

  • 确保使用iText 7.x版本,旧版本API不支持上述配置方式;
  • 若同时使用两种方案,ConverterProperties中的配置优先级高于HTML内的CSS规则;
  • 横向模式下需检查页面内固定宽度的元素,避免内容超出页面范围被截断。

内容的提问来源于stack exchange,提问作者Ramana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:06:19