PDF转图片工具实现

一、安装

bash 复制代码
sudo yum install poppler-utils
pdftoppm -v
pdftoppm -png -r 300 a.pdf /tmp/page

运行效果:

PDF转图片工具 - 在线PDF转PNG/JPG/TIFF转换器 | 免费在线工具

后台实现:

cs 复制代码
using System.Diagnostics;
using System.IO.Compression;

namespace SaaS.OfficialWebSite.Web.Utils
{
    public class PdfTopPmService
    {
        private ILogger<PdfTopPmService> _logger;

        public PdfTopPmService(ILogger<PdfTopPmService> logger)
        {
            _logger = logger;
        }

        public async Task<MemoryStream> ConvertToImagesAsync(MemoryStream pdfStream)
        {
            // 临时保存PDF文件(仅用于转换过程)
            var tempPdfPath = Path.GetTempFileName();
            using (var fileStream = new FileStream(tempPdfPath, FileMode.Create))
            {
                pdfStream.Position = 0;
                await pdfStream.CopyToAsync(fileStream);
                fileStream.Flush();
            }

            // 使用pdftoppm转换PDF为图片
            var outputFiles = ConvertPdfToImages(tempPdfPath);
            // 创建ZIP文件(内存流)
            var zipStream = new MemoryStream();

            using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
            {
                foreach (var imagePath in outputFiles)
                {
                    var entry = archive.CreateEntry(Path.GetFileName(imagePath));
                    using (var entryStream = entry.Open())
                    {
                        using (var imageStream = System.IO.File.OpenRead(imagePath))
                        {
                            await imageStream.CopyToAsync(entryStream);
                        }
                    }
                    // 删除临时图片文件
                    System.IO.File.Delete(imagePath);
                }
            }

            // 删除临时PDF文件
            System.IO.File.Delete(tempPdfPath);
            return zipStream;
        }

        private List<string> ConvertPdfToImages(string pdfPath)
        {
            var outputFiles = new List<string>();
            var outputPattern = Path.Combine(Path.GetTempPath(), "page");

            // 使用pdftoppm命令行工具转换PDF
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "pdftoppm",
                    Arguments = $"-png -r 300 {pdfPath} {outputPattern}",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                }
            };

            process.Start();
            process.WaitForExit();

            // 获取生成的图片文件
            var tempFiles = Directory.GetFiles(Path.GetTempPath(), "page-*.png");
            outputFiles.AddRange(tempFiles);
            return outputFiles;
        }
    }
}
相关推荐
刘某的Cloud10 小时前
列表、元组、字典、集合-组合数据类型
linux·开发语言·python
学烹饪的小胡桃10 小时前
【运维学习】实时性能监控工具 WGCLOUD v3.6.2 更新介绍
linux·运维·服务器·学习·工单系统
知识分享小能手10 小时前
Ubuntu入门学习教程,从入门到精通,Ubuntu 22.04的桌面环境 (4)
linux·学习·ubuntu
叫致寒吧10 小时前
Docker
运维·docker·容器
Lueeee.10 小时前
图解字符驱动模块设计思路
linux
白露与泡影11 小时前
使用systemd,把服务装进 Linux 心脏里~
linux·运维·python
CQ_YM11 小时前
Linux管道通信
linux·c语言·管道·ipc·管道通信
l1t12 小时前
用docker安装oracle 19c
运维·数据库·docker·oracle·容器
k***921613 小时前
【Linux】进程概念(五):详解环境变量的本质
linux·运维·服务器
世转神风-13 小时前
VMware-挂载报错:no mountpoint specified
linux