1

I am working with azure blob storage and usually with large sized scanned pdf documents. I can reduce the size of the documents with GhostscriptProcessor, but first I need to save the document to a physical folder. Because I'm working with blob storage and I have Stream. Can I reduce the size of the documents with stream?

    // "buffer" is file stream that comming from api post
    var ms = new MemoryStream();
    buffer.CopyTo(ms);

    // save file to temp physical folder
    File.WriteAllBytes(inputFile, ms.ToArray());
    ms.Close();

    GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(libPath);
    GhostscriptProcessor proc = new GhostscriptProcessor(gvi);

    List<string> switches = new List<string>();
    switches.Add("-empty");
    switches.Add("-sDEVICE=pdfwrite");
    switches.Add("-dCompatibilityLevel=1.4");
    switches.Add("-dPDFSETTINGS=/ebook");
    switches.Add("-dNOPAUSE");
    switches.Add("-dQUIET");
    switches.Add("-dBATCH");
    switches.Add(@"-sOutputFile=" + outputFile);
    switches.Add(@"-f");
    switches.Add(inputFile);

  // reduce pdf size and save output to temp physical folder
    proc.Process(switches.ToArray());

 // read output file from temp physical folder
    var newStream=File.OpenRead(outputFile);
   ...
   clear temp physical folder and save output file in blob storage...
Fildor
  • 14,510
  • 4
  • 35
  • 67
GAMD
  • 11
  • 2
  • I could imagine that it could work if you redirected stdin of the Ghostscript process to your stream? That would require Ghostscript to be able to work off of the stdin, though. – Fildor Mar 08 '23 at 09:42
  • 1
    PDF files require random access to the content in order to process them, so no, you can't avoid writing the file to disk. While you can pipe the input into stdin all that happens is that Ghostscript then writes the content to its own temporary file. I would think its better to do the work yourself. – KenS Mar 08 '23 at 09:43

0 Answers0