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;
        }
    }
}
相关推荐
大白同学42135 分钟前
【Linux】进程间通信
linux·运维·服务器
..Move...2 小时前
Docker镜像制作
运维·docker·容器
Felven6 小时前
统信系统下设置RTC时间
linux·rtc·1024程序员节
码龄3年 审核中7 小时前
说说SSH的端口转发
大数据·运维·ssh
盛世隐者7 小时前
【Linux】定制Linux系统
linux·运维·服务器
-Excalibur-8 小时前
形象解释关于TCP/IP模型——层层封装MAC数据帧的过程
linux·c语言·网络·笔记·单片机·网络协议·tcp/ip
小跌—8 小时前
Linux:数据链路层
linux·网络
用户31187945592189 小时前
Fedora 37 安装 libicu-71.1-2.fc37.x86_64.rpm 教程(命令行步骤)
linux
搬砖的小码农_Sky9 小时前
Linux(Ubuntu)操作系统下文件的解压
linux·ubuntu
景彡先生10 小时前
Python函数定义与调用全解析:从基础语法到实战技巧
linux·开发语言·python