Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Postscript to Text file using ghostscript

my client wants me to do one task. which is whenever I print ctrl+P from the browser it will go automatically that contents to the database which is sql.

Now, Let me explain what I have tried to achieve this. Usually printerPlusPlus which is third party tool. Which adds virtual printer and prints the files PS to the temp directory than I can read the contents of that postscript file and save it to the database.

My real question is there anything from which I can convert this post script files to text or read them and save the texts to the database? Or is there any better way to achieve this task?

Ghostscript is the alternate and ow-some feature to convert the postscripts to the text or pdf. But, I am completely clueless about the documentation and how to execute their commands.

_viewer.Interpreter.RunFile("C:\\PrinterPlusPlus\\Temp\\ankit_SONY-VAIO_sony_20151227_185020_3.ps");

            GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
            dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            dev.ResolutionXY = new GhostscriptImageDeviceResolution(96, 96);
            dev.InputFiles.Add(@"C:\\PrinterPlusPlus\\Temp\\ankit_SONY-VAIO_sony_20151227_185020_3.ps");
            dev.OutputPath = @"C:\\PrinterPlusPlus\\Temp\\ankit_SONY-VAIO_sony_20151227_185020_3.txt";
            dev.Process();

            _preview.Activate();

I tried this but this seems to be not working and adding ASCII text to the txt file.

like image 217
Just code Avatar asked Oct 19 '22 18:10

Just code


1 Answers

I found ghostscript little confusing. But, I found the solution from here

string inputFile = @"E:\gss_test\test_postscript.ps";
    GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

    // pipe handle format: %handle%hexvalue
    string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

    using (GhostscriptProcessor processor = new GhostscriptProcessor())
    {
        List<string> switches = new List<string>();
        switches.Add("-empty");
        switches.Add("-dQUIET");
        switches.Add("-dSAFER");
        switches.Add("-dBATCH");
        switches.Add("-dNOPAUSE");
        switches.Add("-dNOPROMPT");
        switches.Add("-sDEVICE=pdfwrite");
        switches.Add("-o" + outputPipeHandle);
        switches.Add("-q");
        switches.Add("-f");
        switches.Add(inputFile);

        try
        {
            processor.StartProcessing(switches.ToArray(), null);

            byte[] rawDocumentData = gsPipedOutput.Data;

            //if (writeToDatabase)
            //{
            //    Database.ExecSP("add_document", rawDocumentData);
            //}
            //else if (writeToDisk)
            //{
            //    File.WriteAllBytes(@"E:\gss_test\output\test_piped_output.pdf", rawDocumentData);
            //}
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            gsPipedOutput.Dispose();
            gsPipedOutput = null;
        }
  }

This reads the postscript files easily :)

like image 143
Just code Avatar answered Oct 27 '22 07:10

Just code