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;
        }
    }
}
相关推荐
可涵不会debug2 分钟前
实测ToDesk新终端:跨端会话恢复+多终端并行,远程Vibe Coding的完整体验报告
运维·电脑
忆挽篱笙歌10 分钟前
冯诺依曼+操作系统+进程
linux·ubuntu
资深电气设计1 小时前
TPO卷材研发常见问题:强度与柔韧性为何难兼顾?埃克森美孚威达美™6102的技术参考
运维·物联网·创业创新·业界资讯
优化Henry1 小时前
光路异常告警排查实战:LTE室分与5G小区两个案例解析
运维·网络·学习·5g·tdd
Apipi*1 小时前
30天速通Linux 第九章Linux网络编程
linux·运维·网络
报错小能手1 小时前
Channel面试题。Sync面试题
linux·服务器·windows
天远数科1 小时前
零信任架构实战:基于天远风控经营异常预警构建自动化电子签章前置合规网关
运维·人工智能·架构·自动化
xiaoqiMikko2 小时前
Linux 实战(五):DNS、hosts、CLOSE-WAIT、SSH 权限与改端口 —— 三台真机实测
linux·centos
Linux-lucky2 小时前
43-Linux学习之旅之Shell 脚本(二)
linux·运维·学习·ubuntu·dash
yv798an2 小时前
从 OpenSBI 引导到 C++17 异步运行时:自制 RISC-V 嵌入式专有 NAS 踩坑全记录
linux