private void DoConvert(string[] filenames)
        {
            foreach (string filename in filenames)
            {
                Logging.Info("Processing {0}", filename);
                if (DocumentConversion.CanConvert(filename))
                {
                    string pdf_filename = TempFile.GenerateTempFilename("pdf");
                    DocumentConversion.Convert(filename, pdf_filename);

                    if (ObjAddToGuest.IsChecked ?? false)
                    {
                        Application.Current.Dispatcher.Invoke(
                            new Action(() =>
                        {
                            PDFDocument pdf_document = WebLibraryManager.Instance.Library_Guest.AddNewDocumentToLibrary_SYNCHRONOUS(pdf_filename, filename, null, null, null, true, false);
                            PDFReadingControl pdf_reading_control = MainWindowServiceDispatcher.Instance.OpenDocument(pdf_document);
                            pdf_reading_control.EnableGuestMoveNotification();
                        }),
                            DispatcherPriority.Background);
                    }

                    if (ObjPromptToSave.IsChecked ?? false)
                    {
                        string recommended_filename = TempFile.GenerateTempFilename("pdf");

                        SaveFileDialog save_file_dialog = new SaveFileDialog();
                        save_file_dialog.AddExtension     = true;
                        save_file_dialog.CheckPathExists  = true;
                        save_file_dialog.DereferenceLinks = true;
                        save_file_dialog.OverwritePrompt  = true;
                        save_file_dialog.ValidateNames    = true;
                        save_file_dialog.DefaultExt       = "pdf";
                        save_file_dialog.Filter           = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
                        save_file_dialog.FileName         = recommended_filename;

                        if (true == save_file_dialog.ShowDialog())
                        {
                            string target_filename = save_file_dialog.FileName;
                            File.Copy(pdf_filename, target_filename);
                        }
                    }

                    File.Delete(pdf_filename);
                }
                else
                {
                    StatusManager.Instance.UpdateStatus("DOC_CONV", String.Format("Unable to convert a file of type {0}", Path.GetExtension(filename)));
                }
            }
        }
Exemplo n.º 2
0
        void streamListener_Completed(object sender, EventArgs e)
        {
            try
            {
                StreamListenerTee stream_listener_tee = (StreamListenerTee)sender;

                byte[] captured_data = stream_listener_tee.GetCapturedData();
                if (0 == captured_data.Length)
                {
                    if (!have_notified_about_installing_acrobat)
                    {
                        have_notified_about_installing_acrobat = true;

                        NotificationManager.Instance.AddPendingNotification(
                            new NotificationManager.Notification(
                                "We notice that your PDF files are not loading in your browser.  Please install Acrobat Reader for Qiqqa to be able to automatically add PDFs to your libraries.",
                                "We notice that your PDF files are not loading in your browser.  Please install Acrobat Reader for Qiqqa to be able to automatically add PDFs to your libraries.",
                                NotificationManager.NotificationType.Info,
                                Icons.DocumentTypePdf,
                                "Download",
                                DownloadAndInstallAcrobatReader
                                ));
                    }

                    Logging.Error("We seem to have been notified about a zero-length PDF");
                    return;
                }

                string temp_pdf_filename = TempFile.GenerateTempFilename("pdf");
                File.WriteAllBytes(temp_pdf_filename, captured_data);

                string      pdf_source_url = null; // Can we find this?!!
                PDFDocument pdf_document   = Library.GuestInstance.AddNewDocumentToLibrary_SYNCHRONOUS(temp_pdf_filename, pdf_source_url, null, null, null, true, true);
                File.Delete(temp_pdf_filename);

                Application.Current.Dispatcher.Invoke
                (
                    new Action(() =>
                {
                    PDFReadingControl pdf_reading_control = MainWindowServiceDispatcher.Instance.OpenDocument(pdf_document);
                    pdf_reading_control.EnableGuestMoveNotification(potential_attachment_pdf_document);
                }),
                    DispatcherPriority.Background
                );
            }

            catch (Exception ex)
            {
                Logging.Error(ex, "There was a problem while intercepting the download of a PDF.");
            }
        }
        internal static void GrabWebPage_REMOTE(string title, string url, bool may_try_again_on_exception)
        {
            StatusManager.Instance.UpdateStatus("HTMLToPDF", "Converting HTML to PDF");

            // Strip off the trailing # cos Web2PDF hates is
            if (url.Contains('#'))
            {
                string old_url = url;
                url = url.Substring(0, url.IndexOf('#'));
                Logging.Info("Stripping the # off the original, from '{0}' to '{1}'", old_url, url);
            }

            string filename = Path.GetTempFileName() + ".pdf";

            try
            {
                // Spawn the conversion process
                {
                    string user_override_global = "";
                    string user_override_page   = "";

                    string process_parameters = String.Format(
                        "{0} {1} --footer-right \"Page [page] of [topage]\" {2} --footer-left \"{3}\" --header-left \"{4}\" --header-right \"Created using www.qiqqa.com\" \"{5}\""
                        , user_override_global
                        , url
                        , user_override_page
                        , url.Replace('"', '\'')
                        , title.Replace('"', '\'')
                        , filename
                        );

                    // STDOUT/STDERR
                    using (Process process = ProcessSpawning.SpawnChildProcess(ConfigurationManager.Instance.ProgramHTMLToPDF, process_parameters, ProcessPriorityClass.Normal))
                    {
                        using (ProcessOutputReader process_output_reader = new ProcessOutputReader(process))
                        {
                            process.WaitForExit();

                            Logging.Info("HTMLToPDF:\n{0}", process_output_reader.GetOutputsDumpString());
                        }
                    }
                }

                StatusManager.Instance.UpdateStatus("HTMLToPDF", "Converting HTML to PDF: adding to library");
                PDFDocument pdf_document = Library.GuestInstance.AddNewDocumentToLibrary_SYNCHRONOUS(filename, url, url, null, null, null, true, true);
                pdf_document.Title            = title;
                pdf_document.Year             = Convert.ToString(DateTime.Now.Year);
                pdf_document.DownloadLocation = url;

                WPFDoEvents.InvokeInUIThread(() =>
                {
                    PDFReadingControl pdf_reading_control = MainWindowServiceDispatcher.Instance.OpenDocument(pdf_document);
                    pdf_reading_control.EnableGuestMoveNotification();
                },
                                             priority: DispatcherPriority.Background
                                             );
            }
            catch (Exception ex)
            {
                // Give it a 2nd try...
                if (may_try_again_on_exception)
                {
                    GrabWebPage_REMOTE(title, url, false);
                }
                else
                {
                    throw new UsefulTextException("Problem converting HTML page to PDF.  Please try again later.", String.Format("There has been a problem converting this web page '{0}' with title '{1}' to a PDF.", url, title), ex);
                }
            }
            finally
            {
                FileTools.Delete(filename);
            }

            StatusManager.Instance.UpdateStatus("HTMLToPDF", "Converting HTML to PDF: done");
        }