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;
        }
    }
}
相关推荐
tokepson5 小时前
Mysql下载部署方法备份(Windows/Linux)
linux·服务器·windows·mysql
C_心欲无痕7 小时前
Dockerfile:构建 Docker 镜像
运维·docker·容器
zz_nj8 小时前
工作的环境
linux·运维·服务器
极客先躯8 小时前
如何自动提取Git指定时间段的修改文件?Win/Linux双平台解决方案
linux·git·elasticsearch
C_心欲无痕8 小时前
nginx - 实现域名跳转的几种方式
运维·前端·nginx
suijishengchengde9 小时前
****LINUX时间同步配置*****
linux·运维
幻云20109 小时前
AI自动化编排:从入门到精通(基于Dify构建AI智能系统)
运维·人工智能·自动化
qiuqyue9 小时前
基于虹软Linux Pro SDK的多路RTSP流并发接入、解码与帧级处理实践
linux·运维·网络
切糕师学AI9 小时前
Linux 操作系统简介
linux
南烟斋..10 小时前
GDB调试核心指南
linux·服务器