/// <summary> /// Process the input PDF file by compressing images and/or by converting color to CMYK. The operations /// to perform are established by the constructor. /// </summary> public void ProcessPdfFile(string inputFile, string outputFile) { _inputPdfPath = inputFile; _outputPdfPath = outputFile; var exePath = "/usr/bin/gs"; if (SIL.PlatformUtilities.Platform.IsWindows) { exePath = FindGhostcriptOnWindows(); } if (!String.IsNullOrWhiteSpace(exePath) && File.Exists(exePath)) { if (_worker != null) { _worker.ReportProgress(0, GetSpecificStatus()); } using (var tempPdfFile = TempFile.WithExtension(".pdf")) { var runner = new CommandLineRunner(); var arguments = GetArguments(tempPdfFile.Path); var fromDirectory = String.Empty; var progress = new NullProgress(); // I can't figure out how to use any IProgress based code, but we show progress okay as is. var res = runner.Start(exePath, arguments, Encoding.UTF8, fromDirectory, 3600, progress, ProcessGhostcriptReporting); if (res.DidTimeOut || !RobustFile.Exists(tempPdfFile.Path)) { if (_inputPdfPath != _outputPdfPath) { RobustFile.Copy(_inputPdfPath, _outputPdfPath, true); } return; } // If the process made the file larger and didn't change the color scheme, ignore the result. var oldInfo = new FileInfo(_inputPdfPath); var newInfo = new FileInfo(tempPdfFile.Path); if (newInfo.Length < oldInfo.Length || _type == OutputType.Printshop) { RobustFile.Copy(tempPdfFile.Path, _outputPdfPath, true); } else if (_inputPdfPath != _outputPdfPath) { RobustFile.Copy(_inputPdfPath, _outputPdfPath, true); } } } else { // This shouldn't happen. Linux Bloom package depends on the ghostscript package, and we'll include // ghostscript files in our installer to ensure it's available on Windows. But we have this code here // as a failsafe fallback reminding the developers to ensure this installation work happens. Debug.WriteLine("ghostscript is not installed, so Bloom cannot process the PDF file."); if (_inputPdfPath != _outputPdfPath) { RobustFile.Copy(_inputPdfPath, _outputPdfPath, true); } } }
/// <summary> /// Process the input PDF file by compressing images and/or by converting color to CMYK. The operations /// to perform are established by the constructor. /// </summary> public void ProcessPdfFile(string inputFile, string outputFile, bool removeEvenPages = false) { _inputPdfPath = inputFile; _outputPdfPath = outputFile; var exePath = "/usr/bin/gs"; if (SIL.PlatformUtilities.Platform.IsWindows) { exePath = FindGhostcriptOnWindows(); } if (!String.IsNullOrWhiteSpace(exePath) && File.Exists(exePath)) { if (_worker != null) { _worker.ReportProgress(0, GetSpecificStatus()); } using (var tempPdfFile = TempFile.WithExtension(".pdf")) { var runner = new CommandLineRunner(); var arguments = GetArguments(tempPdfFile.Path, null, removeEvenPages); var fromDirectory = String.Empty; var progress = new NullProgress(); // I can't figure out how to use any IProgress based code, but we show progress okay as is. var res = runner.Start(exePath, arguments, Encoding.UTF8, fromDirectory, 3600, progress, ProcessGhostcriptReporting); if (res.ExitCode != 0) { // On Linux, ghostscript doesn't deal well with some Unicode filenames. Try renaming the input // file temporarily to something innocuous to see if this makes the ghostscript process succeed. // See https://issues.bloomlibrary.org/youtrack/issue/BL-7177. using (var tempInputFile = TempFile.WithExtension(".pdf")) { RobustFile.Delete(tempInputFile.Path); // Move won't replace even empty files. RobustFile.Move(_inputPdfPath, tempInputFile.Path); arguments = GetArguments(tempPdfFile.Path, tempInputFile.Path, removeEvenPages); res = runner.Start(exePath, arguments, Encoding.UTF8, fromDirectory, 3600, progress, ProcessGhostcriptReporting); RobustFile.Move(tempInputFile.Path, _inputPdfPath); } } if (res.ExitCode != 0 || res.DidTimeOut || !RobustFile.Exists(tempPdfFile.Path)) { if (_inputPdfPath != _outputPdfPath) { RobustFile.Copy(_inputPdfPath, _outputPdfPath, true); } return; } // If the process made the file larger and didn't change the color scheme and we're not removing blank pages, ignore the result. var oldInfo = new FileInfo(_inputPdfPath); var newInfo = new FileInfo(tempPdfFile.Path); if (newInfo.Length < oldInfo.Length || _type == OutputType.Printshop || removeEvenPages) { RobustFile.Copy(tempPdfFile.Path, _outputPdfPath, true); } else if (_inputPdfPath != _outputPdfPath) { RobustFile.Copy(_inputPdfPath, _outputPdfPath, true); } } } else { // This shouldn't happen. Linux Bloom package depends on the ghostscript package, and we'll include // ghostscript files in our installer to ensure it's available on Windows. But we have this code here // as a failsafe fallback reminding the developers to ensure this installation work happens. Debug.WriteLine("ghostscript is not installed, so Bloom cannot process the PDF file."); if (_inputPdfPath != _outputPdfPath) { RobustFile.Copy(_inputPdfPath, _outputPdfPath, true); } } }