PDF转图片工具实现

发布于:2025-08-31 ⋅ 阅读:(21) ⋅ 点赞:(0)

一、安装

sudo yum install poppler-utils
pdftoppm -v
pdftoppm -png -r 300 a.pdf /tmp/page

运行效果:

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

后台实现:

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;
        }
    }
}