Ghostscript.NET Rasterizer Out of Memory

I use Ghostscript.NET , a handy C # shell for Ghostscript functionality. I have a package of PDF files sent from the client to convert them to images on the ASP.NET WebAPI server and return to the client.

public static IEnumerable<Image> PdfToImagesGhostscript(byte[] binaryPdfData, int dpi)
{
    List<Image> pagesAsImages = new List<Image>();

    GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(AppDomain.CurrentDomain.BaseDirectory + @"\bin\gsdll32.dll");

    using (var pdfDataStream = new MemoryStream(binaryPdfData))
    using (var rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
    {
        rasterizer.Open(pdfDataStream, gvi, true);

        for (int i = 1; i <= rasterizer.PageCount; i++)
        {
            Image pageAsImage = rasterizer.GetPage(dpi, dpi, i); // Out of Memory Exception on this line
            pagesAsImages.Add(pageAsImage);
        }
    }
    return pagesAsImages;
}

It usually works fine (I usually use 500 dpi, which, as I know, is high, but even when reduced to 300, I can reproduce this error). But if I give him a lot of PDF files from clients (150 one-page PDF files, for example), he often gets into the Out of Memory Exception in the Ghostscript.NET Rasterizer. How can I overcome this? Should it be threaded? If so, how will it work? Would it help to use the 64-bit version of GhostScript? Thanks in advance.

+4
1

, .

, :

for (int page = 1; page <= _rasterizer.PageCount; page++)
{
    var docName = String.Format("Page-{0}.pdf", page);
    var pageFilePath = Path.Combine(outputPath, docName);
    var pdf = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
    pdf.Save(pageFilePath);
    pagesAsImages.Add(pdf);
}

, .

, - . , , GhostscriptProcessor:

private static void GhostscriptNetProcess(String fileName, String outputPath)
{
    var version = Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion();
    var source = (fileName.IndexOf(' ') == -1) ? fileName : String.Format("\"{0}\"", fileName);
    var gsArgs = new List<String>();
    gsArgs.Add("-q");
    gsArgs.Add("-dNOPAUSE");
    gsArgs.Add("-dNOPROMPT");
    gsArgs.Add("-sDEVICE=pdfwrite");
    gsArgs.Add(String.Format(@"-sOutputFile={0}", outputPath));
    gsArgs.Add(source);
    var processor = new Ghostscript.NET.Processor.GhostscriptProcessor(version, false);
    processor.Process(gsArgs.ToArray());
}

, , , :

private static void GhostscriptNetRaster(String fileName, String outputPath)
{
    var version = Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion();
    using (var rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
    {
        rasterizer.Open(File.Open(fileName, FileMode.Open, FileAccess.Read), version, false);
        for (int page = 0; page < rasterizer.PageCount; page++)
        {
            var img = rasterizer.GetPage(96, 96, page);
            img.Save(outputPath);
        }
    }
}

?

0

Source: https://habr.com/ru/post/1614347/


All Articles