0

I am working with transformation of PDF documents into images (jpeg) and I am using GhostScript for this purpose. I work mostly with smaller documents (up to 12 pages) but when I am converting PDFs to images it takes more than 3-7 seconds to do so. Is there any way of how to speed up the GhostScriptNet (C#, ASP.NET)?

This is how I use it right now, works, but quite slowly.

using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
            {
                using (Stream attachmentStream = new MemoryStream(fileData))
                {
                    rasterizer.Open(attachmentStream, version, true);
                    var startPage = GetPage(specificPage, 1);
                    var endPage = GetPage(specificPage, rasterizer.PageCount);
                    for (int i = startPage; i <= endPage; i++)
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            var img = rasterizer.GetPage(120, 120, i);
                            img.Save(ms, ImageFormat.Jpeg);
                            resultImageBase64.Add("data:image/jpeg;base64," + Convert.ToBase64String((byte[])ms.ToArray()));
                        }

                    }
                }
                rasterizer.Dispose();
            }

I further tried to set different Custom switches, but this didn't do the trick either

rasterizer.CustomSwitches = new List<string>()
                    {
                        "-dMaxBitmap=500000000",
                        "-dNumRenderingThreads=24",
                        "-dBufferSpace=500000000",
                        "-dBGPrint=false",
                        "-dNOPAUSE",
                        "-dNOPROMPT",
                        "-r120", //120dpi
                        "-dAlignToPixels=0",
                        "-dGridFitTT=0",
                        "-dBATCH",
                        "-dNOEPS",
                    };
L.V
  • 151
  • 1
  • 3
  • 9
  • 3-7 seconds is relative to your development machine. 3-7 seconds running on a pentium 2 w/ windows 95 is most likely brilliant performance. What benchmarking have you done? What is an "acceptable speed" for your environment? If I have a pdf with 12 pages of 12 terabyte images, its obviously going to change the compute time again. At the moment I am voting to close this question as too broad. – Zze Sep 11 '19 at 06:51
  • I've done tests locally (i7-8750H) and also on the production (azure with 12 cores) so I guess machine should not be the problem. Files that I use are in a size of 3-5MB. I think that acceptable would be to squeeze it under 1 second. – L.V Sep 11 '19 at 07:00
  • What evidence do you have that the same process in the same environment can be achieved more permanently i.e. maybe it just takes that long ? – Zze Sep 11 '19 at 11:11
  • Yeah maybe you are right. Well I thought that maybe someone has some more experience with "performance tricks" that I could try. – L.V Sep 11 '19 at 11:24
  • Honestly, in my opinion, 7 seconds to read from a doc, process and then create files seems more than reasonable - especially if you are giving the user feedback. Maybe through web sockets or whatever tech you have available. "Reading PDF...", "Generating images..." , "Exporting..." etc. I don't think this is a bad question, it's just such a hard one to answer because there are so many variables. – Zze Sep 11 '19 at 11:45

0 Answers0