多张图片生成PDF每张图片生成pdf的一页

发布于:2025-06-28 ⋅ 阅读:(57) ⋅ 点赞:(0)

需要的库

  <!-- 生成pdf-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-net/commons-net FTPClient -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>

代码一,生成本地pdf文件

if (list.size() > 0) {

            Document document = new Document();
            FTPClient ftp = new FTPClient();
            try {
                //1、创建一个document对象
                //2、创建一个PdfWriter实例
                FileData fileData1=list.get(0);
                String filenamePdf=fileData1.getCatalogname()+fileData1.getCatalogid()+".pdf";
                PdfWriter.getInstance(document, new FileOutputStream(filenamePdf));
                //3、打开文档
                document.open();
                int pagenum = 0;
                for (FileData filePage : list) {
                    //页码
                    pagenum++;
                    String folder = filePage.getFolder();
                    //服务器主机
                    String host = filePage.getHost();
                    //服务器端口
                    int port = Integer.parseInt(filePage.getPort());
                    //登录用户名
                    String username = filePage.getUsername();
                    //登录密码
                    String pwd = filePage.getPassword();
                    ftp.setControlEncoding(filePage.getEncoding());
                    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
                    ftp.connect(host, port);
                    if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        ftp.disconnect();
                    }
                    ftp.login(username, pwd);
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                    ftp.enterLocalPassiveMode();
                    ftp.changeWorkingDirectory(folder);
                    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ftp.retrieveFile(filePage.getFilename(), bos);
                    Image img = Image.getInstance(bos.toByteArray());
                    img.setAlignment(Image.ALIGN_CENTER);
                    // 根据图片大小设置页面,一定要先设置页面,再newPage(),否则无效
                    document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
                    document.newPage();
                    document.add(img);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    ftp.logout();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (document.isOpen()) {
                    document.close();
                }
            }

        }

代码二、生成pdf直接发送到响应中

上方代码加入和替换此两行,请求添加 在游览器可以直接预览,页面调用打印方法
页面参数直接在后端取

 response.setContentType("application/pdf;charset=utf-8");
 PdfWriter.getInstance(document, response.getOutputStream());
<body>
<iframe style="display: none;" id="printIframe" src="/controllers/viewimagecontroller/printImageByPageIds.do"></iframe>
<script type="text/javascript">
    $(function () {
        doPrint();
    });
    //点击打印按钮,触发事件】
    function doPrint(){
        $("#printIframe")[0].contentWindow.print();
    }
</script>
</body>

网站公告

今日签到

点亮在社区的每一天
去签到