Inheritance: ConversionEngine
        public override ConversionEngine ConstructFileEngine(string fileName, byte[] file,
            UnsupportedTypeHandlingEnum unsupportedHandling)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new MissingSourceException("File not specified.");
            }

            ConversionEngine engine = null;
            switch (Path.GetExtension(fileName).ToLower())
            {
                case ".pdf":
                    engine = new PdfEngine(file);
                    break;
                case ".png":
                case ".gif":
                case ".jpeg":
                case ".jpg":
                case ".bmp":
                    engine = new WebKitImageEngine(fileName, file);
                    break;
                case ".html":
                case ".htm":
                    engine = new WebKitFileEngine(fileName, file);
                    break;
                case ".odt":
                case ".doc":
                case ".docx":
                case ".xls":
                case ".xlsx":
                case ".ppt":
                case ".pptx":
                case ".txt":
                case ".rtf":
                    engine = new LibreOfficeConversionEngine(fileName, file);
                    break;
                default:
                    switch (unsupportedHandling)
                    {
                        case UnsupportedTypeHandlingEnum.Error:
                            throw new UnsupportedSourceException("Unsupported file type.", Path.GetExtension(fileName));
                        case UnsupportedTypeHandlingEnum.NoPage:
                            engine = new NullEngine();
                            break;
                        case UnsupportedTypeHandlingEnum.PageWithErrorText:
                            engine = new UnsupportedTypeEngine(fileName);
                            break;
                        default:
                            throw new InvalidEnumArgumentException("unsupportedHandling", (int)unsupportedHandling, typeof(UnsupportedTypeHandlingEnum));
                    }
                    break;
            }

            return engine;
        }
Beispiel #2
0
        public override ConversionEngine ConstructFileEngine(string fileName, byte[] file,
                                                             UnsupportedTypeHandlingEnum unsupportedHandling)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new MissingSourceException("File not specified.");
            }

            ConversionEngine engine = null;

            switch (Path.GetExtension(fileName).ToLower())
            {
            case ".pdf":
                engine = new PdfEngine(file);
                break;

            case ".png":
            case ".gif":
            case ".jpeg":
            case ".jpg":
            case ".bmp":
                engine = new WebKitImageEngine(fileName, file);
                break;

            case ".html":
            case ".htm":
                engine = new WebKitFileEngine(fileName, file);
                break;

            case ".odt":
            case ".doc":
            case ".docx":
            case ".xls":
            case ".xlsx":
            case ".ppt":
            case ".pptx":
            case ".txt":
            case ".rtf":
                engine = new LibreOfficeConversionEngine(fileName, file);
                break;

            default:
                switch (unsupportedHandling)
                {
                case UnsupportedTypeHandlingEnum.Error:
                    throw new UnsupportedSourceException("Unsupported file type.", Path.GetExtension(fileName));

                case UnsupportedTypeHandlingEnum.NoPage:
                    engine = new NullEngine();
                    break;

                case UnsupportedTypeHandlingEnum.PageWithErrorText:
                    engine = new UnsupportedTypeEngine(fileName);
                    break;

                default:
                    throw new InvalidEnumArgumentException("unsupportedHandling", (int)unsupportedHandling, typeof(UnsupportedTypeHandlingEnum));
                }
                break;
            }

            return(engine);
        }
        protected override void Convert(ConversionOptions options)
        {
            string inputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(m_sourceFileName));
            File.WriteAllBytes(inputFile, m_sourceFile);
            AddFileToSweep(inputFile);

            Process conversionProcess = new Process();
            conversionProcess.StartInfo.UseShellExecute = false;
            conversionProcess.StartInfo.ErrorDialog = false;
            conversionProcess.StartInfo.CreateNoWindow = true;
            conversionProcess.StartInfo.RedirectStandardOutput = true;
            conversionProcess.StartInfo.RedirectStandardError = true;
            conversionProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(inputFile);
            conversionProcess.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LIBREOFFICE_RELATIVE_PATH);
            conversionProcess.StartInfo.Arguments += "--headless -convert-to pdf " + Path.GetFileName(inputFile);

            string outputFile = Path.Combine(Path.GetDirectoryName(inputFile), string.Concat(Path.GetFileNameWithoutExtension(inputFile), ".pdf"));
            AddFileToSweep(outputFile);

            string standardErrorOutput = string.Empty;

            try
            {
                lock (m_processLock) //only 1 instance of libre office can be running at a time
                {
                    s_logger.Debug(string.Format("Starting process: {0} with arguments: {1}", LIBREOFFICE_RELATIVE_PATH, conversionProcess.StartInfo.Arguments));
                    conversionProcess.Start();
                    standardErrorOutput = conversionProcess.StandardError.ReadToEnd();
                    int timeoutMilliseconds = options.TimeoutInSeconds > 0 ? options.TimeoutInSeconds * 1000 : DEFAULT_TIMEOUT_MILLISECONDS;
                    if (!conversionProcess.WaitForExit(timeoutMilliseconds))
                    {
                        conversionProcess.Kill();
                        throw new ConversionTimeoutException(string.Format("Conversion timeout after {0} milliseconds.", timeoutMilliseconds.ToString()), standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                    }
                    else if (conversionProcess.ExitCode != 0)
                    {
                        string message = "Error when running conversion process: " + LIBREOFFICE_RELATIVE_PATH;
                        throw new ConversionEngineException(message, standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                    }
                }

                this.OutputFile = File.ReadAllBytes(outputFile);

                if (conversionProcess.ExitCode != 0)
                {
                    throw new ConversionEngineException("Error when running conversion process: " + LIBREOFFICE_RELATIVE_PATH, standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                }

                if (!File.Exists(outputFile))
                {
                    throw new ConversionEngineException("Output Pdf file missing after conversion.", standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                }
                else
                {
                    this.OutputFile = File.ReadAllBytes(outputFile);

                    if (options.Orientation == PageOrientation.Landscape)
                    {
                        //change Orientation
                        using (PdfEngine engine = new PdfEngine(this.OutputFile, true))
                        {
                            engine.ConvertToPdf(options);
                            this.OutputFile = engine.OutputFile;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ConversionEngineException("Error when converting Pdf with process: " + LIBREOFFICE_RELATIVE_PATH, standardErrorOutput, Path.GetExtension(m_sourceFileName), this, ex);
            }
        }
        protected override void Convert(ConversionOptions options)
        {
            string inputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(m_sourceFileName));

            File.WriteAllBytes(inputFile, m_sourceFile);
            AddFileToSweep(inputFile);

            Process conversionProcess = new Process();

            conversionProcess.StartInfo.UseShellExecute        = false;
            conversionProcess.StartInfo.ErrorDialog            = false;
            conversionProcess.StartInfo.CreateNoWindow         = true;
            conversionProcess.StartInfo.RedirectStandardOutput = true;
            conversionProcess.StartInfo.RedirectStandardError  = true;
            conversionProcess.StartInfo.WorkingDirectory       = Path.GetDirectoryName(inputFile);
            conversionProcess.StartInfo.FileName   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LIBREOFFICE_RELATIVE_PATH);
            conversionProcess.StartInfo.Arguments += "--headless -convert-to pdf " + Path.GetFileName(inputFile);

            string outputFile = Path.Combine(Path.GetDirectoryName(inputFile), string.Concat(Path.GetFileNameWithoutExtension(inputFile), ".pdf"));

            AddFileToSweep(outputFile);

            string standardErrorOutput = string.Empty;

            try
            {
                lock (m_processLock) //only 1 instance of libre office can be running at a time
                {
                    s_logger.Debug(string.Format("Starting process: {0} with arguments: {1}", LIBREOFFICE_RELATIVE_PATH, conversionProcess.StartInfo.Arguments));
                    conversionProcess.Start();
                    standardErrorOutput = conversionProcess.StandardError.ReadToEnd();
                    int timeoutMilliseconds = options.TimeoutInSeconds > 0 ? options.TimeoutInSeconds * 1000 : DEFAULT_TIMEOUT_MILLISECONDS;
                    if (!conversionProcess.WaitForExit(timeoutMilliseconds))
                    {
                        conversionProcess.Kill();
                        throw new ConversionTimeoutException(string.Format("Conversion timeout after {0} milliseconds.", timeoutMilliseconds.ToString()), standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                    }
                    else if (conversionProcess.ExitCode != 0)
                    {
                        string message = "Error when running conversion process: " + LIBREOFFICE_RELATIVE_PATH;
                        throw new ConversionEngineException(message, standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                    }
                }

                this.OutputFile = File.ReadAllBytes(outputFile);

                if (conversionProcess.ExitCode != 0)
                {
                    throw new ConversionEngineException("Error when running conversion process: " + LIBREOFFICE_RELATIVE_PATH, standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                }

                if (!File.Exists(outputFile))
                {
                    throw new ConversionEngineException("Output Pdf file missing after conversion.", standardErrorOutput, Path.GetExtension(m_sourceFileName), this);
                }
                else
                {
                    this.OutputFile = File.ReadAllBytes(outputFile);

                    if (options.Orientation == PageOrientation.Landscape)
                    {
                        //change Orientation
                        using (PdfEngine engine = new PdfEngine(this.OutputFile, true))
                        {
                            engine.ConvertToPdf(options);
                            this.OutputFile = engine.OutputFile;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ConversionEngineException("Error when converting Pdf with process: " + LIBREOFFICE_RELATIVE_PATH, standardErrorOutput, Path.GetExtension(m_sourceFileName), this, ex);
            }
        }