protected void ConvertToPdf(string[] urls, ConversionOptions options) { string outputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".pdf"); AddFileToSweep(outputFile); 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 = AppDomain.CurrentDomain.BaseDirectory; conversionProcess.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, WKHTMLTOPDF_RELATIVE_PATH); string optionArguments = GenerateOptionsArguments(options); conversionProcess.StartInfo.Arguments = optionArguments; conversionProcess.StartInfo.Arguments += " " + string.Join(" ", urls); conversionProcess.StartInfo.Arguments += " " + outputFile; string standardErrorOutput = string.Empty; try { s_logger.Debug(string.Format("Starting process: {0} with arguments: {1}", WKHTMLTOPDF_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, "Url", this); } else if (conversionProcess.ExitCode != 0) { if (standardErrorOutput.Contains("Failed loading page")) { throw new UrlFetchException(urls, standardErrorOutput, this); } else { throw new ConversionEngineException("Error when running conversion process: " + WKHTMLTOPDF_RELATIVE_PATH, standardErrorOutput, "Url", this); } } if (!File.Exists(outputFile)) { throw new ConversionEngineException("Output Pdf file missing after conversion.", standardErrorOutput, "Url", this); } else { this.OutputFile = File.ReadAllBytes(outputFile); } } catch (Exception ex) { throw new ConversionEngineException("Error when converting Pdf with process: " + WKHTMLTOPDF_RELATIVE_PATH, standardErrorOutput, "Url", this, ex); } }
public byte[] ConvertFile(byte[] file, string fileName, ConversionOptions options) { if (file == null || file.Length == 0) { throw new MissingSourceException("File not specified."); } return ConvertFiles(new Dictionary<string, byte[]>() { { fileName, file } }, options); }
public byte[] ConvertFile(byte[] file, string fileName, ConversionOptions options) { if (file == null || file.Length == 0) { throw new MissingSourceException("File not specified."); } return(ConvertFiles(new Dictionary <string, byte[]>() { { fileName, file } }, options)); }
protected override void Convert(ConversionOptions options) { List<string> sourceFiles = new List<string>(); foreach (KeyValuePair<string, byte[]> sourceFile in m_sourceFiles) { string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(sourceFile.Key)); File.WriteAllBytes(tempFile, sourceFile.Value); sourceFiles.Add(tempFile); AddFileToSweep(tempFile); } ConvertToPdf(sourceFiles.ToArray(), options); }
public byte[] CombinePdfs(List <byte[]> sourcePdfs, ConversionOptions options) { if (sourcePdfs == null || sourcePdfs.Count == 0) { throw new MissingSourceException("No pdfs were specified."); } using (ConversionEngine engine = m_engineFactory.ConstructPdfEngine(sourcePdfs)) { engine.ConvertToPdf(options); return(engine.OutputFile); } }
public byte[] CombinePdfs(List<byte[]> sourcePdfs, ConversionOptions options) { if (sourcePdfs == null || sourcePdfs.Count == 0) { throw new MissingSourceException("No pdfs were specified."); } using (ConversionEngine engine = m_engineFactory.ConstructPdfEngine(sourcePdfs)) { engine.ConvertToPdf(options); return engine.OutputFile; } }
protected override void Convert(ConversionOptions options) { List <string> sourceFiles = new List <string>(); foreach (KeyValuePair <string, byte[]> sourceFile in m_sourceFiles) { string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(sourceFile.Key)); File.WriteAllBytes(tempFile, sourceFile.Value); sourceFiles.Add(tempFile); AddFileToSweep(tempFile); } ConvertToPdf(sourceFiles.ToArray(), options); }
protected override void Convert(ConversionOptions options) { if (m_sourceFiles != null) { if (m_sourceFiles.Count == 1 && !m_forceConversion) { this.OutputFile = m_sourceFiles[0]; } else { try { using (MemoryStream ms = new MemoryStream()) { using (Document doc = new Document(options.Orientation == PageOrientation.Portrait ? PageSize.LETTER : PageSize.LETTER_LANDSCAPE)) { PdfCopy copy = new PdfCopy(doc, ms); PdfReader reader; doc.Open(); int n; foreach (byte[] file in m_sourceFiles) { if (file != null) { reader = new PdfReader(file); n = reader.NumberOfPages; for (int page = 1; page <= n; page++) { copy.AddPage(copy.GetImportedPage(reader, page)); } copy.FreeReader(reader); } } } this.OutputFile = ms.ToArray(); } } catch (Exception ex) { throw new ConversionEngineException("Error when merging Pdf files.", null, "pdf", this, ex); } } } }
private UnsupportedTypeHandlingEnum GetUnsupportedHandlingFromOptions(ConversionOptions options) { UnsupportedTypeHandlingEnum handling = UnsupportedTypeHandlingEnum.Error; if (!options.SilentFailOnUnsupportedType) { handling = UnsupportedTypeHandlingEnum.Error; } else if (options.UsePlaceholderPageOnUnsupportedType) { handling = UnsupportedTypeHandlingEnum.PageWithErrorText; } else { handling = UnsupportedTypeHandlingEnum.NoPage; } return(handling); }
protected override void Convert(ConversionOptions options) { List <string> sourceFiles = new List <string>(); foreach (KeyValuePair <string, byte[]> sourceFile in m_sourceFiles) { string tempImageFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(sourceFile.Key)); File.WriteAllBytes(tempImageFile, sourceFile.Value); AddFileToSweep(tempImageFile); string widthAttribute = string.Empty; string heightAttribute = string.Empty; if (options != null && options is ImageConversionOptions) { ImageConversionOptions imageOptions = (ImageConversionOptions)options; if (imageOptions.ImageWidthPixelsMin.HasValue) { widthAttribute = string.Format("width='{0}'", imageOptions.ImageWidthPixelsMin.Value); } if (imageOptions.ImageHeightPixelsMin.HasValue) { heightAttribute = string.Format("width='{0}'", imageOptions.ImageHeightPixelsMin.Value); } } string tempHtmlFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".html"); string tempHtmlFileContent = string.Format("<html><body><img src='{0}' {1} {2} /></body></html>", tempImageFile, widthAttribute, heightAttribute); File.WriteAllText(tempHtmlFile, tempHtmlFileContent); sourceFiles.Add(tempHtmlFile); AddFileToSweep(tempHtmlFile); } string optionArguments = GenerateOptionsArguments(options); ConvertToPdf(sourceFiles.ToArray(), options); }
public byte[] ConvertFiles(Dictionary<string, byte[]> files, ConversionOptions options) { if (files == null || files.Count == 0) { throw new MissingSourceException("No files were specified."); } List<byte[]> converted = new List<byte[]>(); foreach (KeyValuePair<string, byte[]> file in files) { UnsupportedTypeHandlingEnum unsupportedHandling = GetUnsupportedHandlingFromOptions(options); using (ConversionEngine engine = m_engineFactory.ConstructFileEngine(file.Key, file.Value, unsupportedHandling)) { engine.ConvertToPdf(options); byte[] result = engine.OutputFile; converted.Add(result); } } return CombinePdfs(converted, options); }
public byte[] ConvertFiles(Dictionary <string, byte[]> files, ConversionOptions options) { if (files == null || files.Count == 0) { throw new MissingSourceException("No files were specified."); } List <byte[]> converted = new List <byte[]>(); foreach (KeyValuePair <string, byte[]> file in files) { UnsupportedTypeHandlingEnum unsupportedHandling = GetUnsupportedHandlingFromOptions(options); using (ConversionEngine engine = m_engineFactory.ConstructFileEngine(file.Key, file.Value, unsupportedHandling)) { engine.ConvertToPdf(options); byte[] result = engine.OutputFile; converted.Add(result); } } return(CombinePdfs(converted, options)); }
protected override void Convert(ConversionOptions options) { List<string> sourceFiles = new List<string>(); foreach (KeyValuePair<string, byte[]> sourceFile in m_sourceFiles) { string tempImageFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.GetExtension(sourceFile.Key)); File.WriteAllBytes(tempImageFile, sourceFile.Value); AddFileToSweep(tempImageFile); string widthAttribute = string.Empty; string heightAttribute = string.Empty; if (options != null && options is ImageConversionOptions) { ImageConversionOptions imageOptions = (ImageConversionOptions)options; if (imageOptions.ImageWidthPixelsMin.HasValue) { widthAttribute = string.Format("width='{0}'", imageOptions.ImageWidthPixelsMin.Value); } if (imageOptions.ImageHeightPixelsMin.HasValue) { heightAttribute = string.Format("width='{0}'", imageOptions.ImageHeightPixelsMin.Value); } } string tempHtmlFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".html"); string tempHtmlFileContent = string.Format("<html><body><img src='{0}' {1} {2} /></body></html>", tempImageFile, widthAttribute, heightAttribute); File.WriteAllText(tempHtmlFile, tempHtmlFileContent); sourceFiles.Add(tempHtmlFile); AddFileToSweep(tempHtmlFile); } string optionArguments = GenerateOptionsArguments(options); ConvertToPdf(sourceFiles.ToArray(), options); }
public byte[] ConvertZipFile(byte[] zipFile, ConversionOptions options) { if (zipFile == null || zipFile.Length == 0) { throw new MissingSourceException("No zip file was specified."); } Dictionary<string, byte[]> files = new Dictionary<string, byte[]>(); using (MemoryStream sourceStream = new MemoryStream(zipFile)) { using (ZipFile zip = ZipFile.Read(sourceStream)) { foreach (ZipEntry e in zip) { using (MemoryStream targetStream = new MemoryStream()) { e.Extract(targetStream); files.Add(e.FileName, targetStream.ToArray()); } } } } return ConvertFiles(files, options); }
protected override void Convert(ConversionOptions options) { this.OutputFile = null; }
protected abstract void Convert(ConversionOptions options);
public void ConvertToPdf(ConversionOptions options) { Convert(options); }
protected virtual string GenerateOptionsArguments(ConversionOptions options) { StringBuilder arguments = new StringBuilder(); if (options != null) { arguments.Append("--orientation ").Append(options.Orientation.ToString()); if (options is HtmlConversionOptions) { HtmlConversionOptions htmlOptions = (HtmlConversionOptions)options; if (!string.IsNullOrEmpty(htmlOptions.HTTPAuthenticationUsername)) { arguments.Append(" --username ").Append(htmlOptions.HTTPAuthenticationUsername); arguments.Append(" --password ").Append(htmlOptions.HTTPAuthenticationPassword); } if (htmlOptions.PrintMediaType) { arguments.Append(" --print-media-type"); } if (!string.IsNullOrEmpty(htmlOptions.UserStyleSheetUrl)) { arguments.Append(" --user-style-sheet ").Append(htmlOptions.UserStyleSheetUrl); } } } return arguments.ToString(); }
protected override void Convert(ConversionOptions options) { ConvertToPdf(m_sourceUrls, options); }
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); } }
public byte[] ConvertText(string text, ConversionOptions options) { using (ConversionEngine engine = m_engineFactory.ConstructTextEngine(text)) { engine.ConvertToPdf(options); return engine.OutputFile; } }
private UnsupportedTypeHandlingEnum GetUnsupportedHandlingFromOptions(ConversionOptions options) { UnsupportedTypeHandlingEnum handling = UnsupportedTypeHandlingEnum.Error; if (!options.SilentFailOnUnsupportedType) { handling = UnsupportedTypeHandlingEnum.Error; } else if (options.UsePlaceholderPageOnUnsupportedType) { handling = UnsupportedTypeHandlingEnum.PageWithErrorText; } else { handling = UnsupportedTypeHandlingEnum.NoPage; } return handling; }