Ejemplo n.º 1
0
        protected IEnumerable <string> SetDeviceCommand(string outputfilename, string jobname, string driver)
        {
            yield return("mark");

            yield return("/NoCancel");

            yield return("true");

            yield return("/OutputFile");

            yield return(PostscriptHelper.EscapePostscriptString(outputfilename));

            yield return("/UserSettings");

            yield return("<<");

            yield return("/DocumentName");

            yield return(PostscriptHelper.EscapePostscriptString(jobname));

            yield return(">>");

            yield return(PostscriptHelper.EscapePostscriptString(driver));

            yield return("finddevice");

            yield return("putdeviceprops");

            yield return("setdevice");

            yield return(".setsafe");
        }
Ejemplo n.º 2
0
        protected byte[] ProcessData(string username, PrintTicket ticket, byte[] data, string[] inargs, string[] extraargs, string[] devsetup)
        {
            string[] pagesetup = PostscriptHelper.SetPageDeviceCommand(ticket).ToArray();

            string[] args = new string[] { "-dNOPAUSE", "-dBATCH" }
            .Concat(inargs ?? new string[] { })
            .Concat(extraargs ?? new string[] { })
            .Concat(new string[] { "-sOutputFile=%stdout" })
            .Concat(new string[] { "-c" })
            .Concat(devsetup ?? new string[] { })
            .Concat(pagesetup)
            .Concat(new string[] { "-" })
            .ToArray();

            MemoryStream outstream = new MemoryStream();
            MemoryStream errstream = new MemoryStream();
            MemoryStream instream  = new MemoryStream(data);

            int exitcode;

            if (username != null)
            {
                exitcode = RunCommandAsUser(username, args, instream, outstream, errstream);
            }
            else
            {
                exitcode = RunCommand(args, instream, outstream, errstream);
            }

            string errstr = Encoding.UTF8.GetString(errstream.ToArray());

            if (exitcode != 0)
            {
                Logger.Log(LogLevel.Warning, "Ghostscript returned code {0}\n\nError:\n{1}", exitcode, errstr);
                throw new InvalidOperationException(String.Format("Ghostscript error {0}\n{1}", exitcode, errstr));
            }

            return(outstream.ToArray());
        }
Ejemplo n.º 3
0
        public static PaginatedPrintData FromPDF(byte[] PDFData, PrintTicket ticket)
        {
            List <byte> pagesetup = PostscriptHelper.SetPageDeviceCommand(ticket).SelectMany(w =>
            {
                List <byte> wb = Encoding.ASCII.GetBytes(w).ToList();
                wb.Add((byte)' ');
                return(wb);
            }).ToList();

            pagesetup.Add((byte)'\n');

            byte[] psdata = PostscriptHelper.FromPDF(PDFData);

            bool          inprologue = true;
            List <byte[]> pages      = new List <byte[]>();
            List <byte>   prologue   = new List <byte>();

            byte[] epilogue = null;

            byte[] pdfsetup = Encoding.ASCII.GetBytes("false pdfSetup");
            byte[] pagesep  = Encoding.ASCII.GetBytes("%%Page:");
            byte[] trailer  = Encoding.ASCII.GetBytes("%%Trailer");
            int    start    = 0;

            for (int i = 0; i < psdata.Length; i++)
            {
                if (i == 0 || psdata[i - 1] == '\n')
                {
                    if (inprologue && i < psdata.Length - pdfsetup.Length && pdfsetup.Select((v, j) => psdata[i + j] == v).All(v => v))
                    {
                        byte[] _prologue = new byte[i - start];
                        Array.Copy(psdata, start, _prologue, 0, i - start);
                        prologue.AddRange(_prologue);
                        prologue.AddRange(pagesetup);
                        i    += pdfsetup.Length;
                        start = i;
                    }
                    else if (i < psdata.Length - pagesep.Length && pagesep.Select((v, j) => psdata[i + j] == v).All(v => v))
                    {
                        if (inprologue)
                        {
                            byte[] _prologue = new byte[i - start];
                            Array.Copy(psdata, start, _prologue, 0, i - start);
                            prologue.AddRange(_prologue);
                            inprologue = false;
                            start      = i;
                        }
                        else
                        {
                            byte[] page = new byte[i - start];
                            Array.Copy(psdata, start, page, 0, i - start);
                            pages.Add(page);
                            start = i;
                        }
                    }
                    else if (i < psdata.Length - trailer.Length && trailer.Select((v, j) => psdata[i + j] == v).All(v => v))
                    {
                        byte[] page = new byte[i - start];
                        Array.Copy(psdata, start, page, 0, i - start);
                        pages.Add(page);
                        epilogue = new byte[psdata.Length - i];
                        Array.Copy(psdata, i, epilogue, 0, psdata.Length - i);
                        break;
                    }
                }
            }

            return(new PaginatedPrintData
            {
                Prologue = prologue.ToArray(),
                PageData = pages.ToArray(),
                Epilogue = epilogue
            });
        }