コード例 #1
0
ファイル: GeckofxHtmlToPdf.cs プロジェクト: psy0370/Html2Pdf
        /// <summary>
        /// PDF保存開始処理
        /// </summary>
        private void StartMakingPdf()
        {
            nsIWebBrowserPrint      print       = Xpcom.QueryInterface <nsIWebBrowserPrint>(gecko.Window.DomWindow);
            nsIPrintSettingsService prtService  = Xpcom.GetService <nsIPrintSettingsService>("@mozilla.org/gfx/printsettings-service;1");
            nsIPrintSettings        prtSettings = prtService.GetNewPrintSettingsAttribute();

            prtSettings.SetToFileNameAttribute(tempPdf);        // 保存PDFファイル名
            prtSettings.SetPrintSilentAttribute(true);          // サイレントモード
            prtSettings.SetShowPrintProgressAttribute(false);   // 印刷進捗非表示

            prtSettings.SetPaperHeightAttribute(297.0 * ipm);   // 高さ設定 A4
            prtSettings.SetPaperWidthAttribute(210.0 * ipm);    // 幅設定  A4

            prtSettings.SetMarginTopAttribute(5.08 / mpi);      // 上余白設定
            prtSettings.SetMarginBottomAttribute(5.18 / mpi);   // 下余白
            prtSettings.SetMarginLeftAttribute(5.08 / mpi);     // 左余白
            prtSettings.SetMarginRightAttribute(5.1 / mpi);     // 右余白

            prtSettings.SetHeaderStrLeftAttribute("");          // ヘッダー 左
            prtSettings.SetHeaderStrCenterAttribute("");        // ヘッダー 中
            prtSettings.SetHeaderStrRightAttribute("");         // ヘッダー 右
            prtSettings.SetFooterStrLeftAttribute("");          // フッター 左
            prtSettings.SetFooterStrCenterAttribute("");        // フッター 中
            prtSettings.SetFooterStrRightAttribute("");         // フッター 右
            prtSettings.SetDownloadFontsAttribute(false);

            prtSettings.SetPrintBGColorsAttribute(true);
            prtSettings.SetPrintBGImagesAttribute(true);

            prtSettings.SetOrientationAttribute(nsIPrintSettingsConsts.kPortraitOrientation);   // 用紙向き
            prtSettings.SetOutputFormatAttribute(nsIPrintSettingsConsts.kOutputFormatPDF);      // 保存フォーマット PDF

            print.Print(prtSettings, this);
            finishedTimer.Enabled = true;
        }
コード例 #2
0
        private void FinishMakingPdf()
        {
            if (_conversionOrder.ReportMemoryUsage)
            {
                Console.WriteLine("Making the PDF took {0}", DateTime.Now - _startMakingPdf);
                MemoryManagement.CheckMemory(false, "Memory use after printing", false);
            }
            if (_conversionOrder.ReduceMemoryUse)
            {
                if (!File.Exists(_currentFile))
                {
                    throw new ApplicationException(string.Format(
                                                       "GeckoFxHtmlToPdf was not able to create the PDF file ({0}).{1}{1}Details: Gecko did not produce the expected document.",
                                                       _currentFile, Environment.NewLine));
                }
                // collect all the memory we can between pages
                GC.Collect();
                GC.WaitForPendingFinalizers();
                MemoryService.MinimizeHeap(true);
                _browser.Window.WindowUtils.GarbageCollect(null /*hopefully nulls ok*/, 0);

                var length = new FileInfo(_currentFile).Length;
                if (IsPrintingFinished(length))
                {
                    CombinePageFilesTogether();
                    if (_conversionOrder.ReportMemoryUsage)
                    {
                        MemoryManagement.CheckMemory(false, "Memory use after combining all the pages", false);
                        Console.WriteLine("Making all the PDF pages took {0}", DateTime.Now - _beginPages);
                    }
                    RaiseFinished();
                    return;
                }
                ++_currentPage;
                _printSettings.SetStartPageRangeAttribute(_currentPage);
                _printSettings.SetEndPageRangeAttribute(_currentPage);
                _currentFile = String.Format("{0}-page{1:000}", _pathToTempPdf, _currentPage);
                _printSettings.SetToFileNameAttribute(_currentFile);
                _finished       = false;
                _startMakingPdf = DateTime.Now;
                Status          = String.Format("Making Page {0} of PDF...", _currentPage);
                RaiseStatusChanged(new PdfMakingStatus()
                {
                    percentage = 0, statusLabel = Status
                });
                _print.Print(_printSettings, this);
                _checkForPdfFinishedTimer.Enabled = true;
                return;
            }

            if (!File.Exists(_pathToTempPdf))
            {
                throw new ApplicationException(string.Format(
                                                   "GeckoFxHtmlToPdf was not able to create the PDF file ({0}).{1}{1}Details: Gecko did not produce the expected document.",
                                                   _pathToTempPdf, Environment.NewLine));
            }

            try
            {
                File.Move(_pathToTempPdf, _conversionOrder.OutputPdfPath);
                RaiseFinished();
            }
            catch (IOException e)
            {
                // We can get here for a different reason: the source file is still in use
                throw new ApplicationException(
                          string.Format(
                              "Tried to move the file {0} to {1}, but the Operating System said that one of these files was locked. Please try again.{2}{2}Details: {3}",
                              _pathToTempPdf, _conversionOrder.OutputPdfPath, Environment.NewLine, e.Message));
            }
        }
コード例 #3
0
        private void StartMakingPdf()
        {
            _print = Xpcom.QueryInterface <nsIWebBrowserPrint>(_browser.Window.DomWindow);

            var service = Xpcom.GetService <nsIPrintSettingsService>("@mozilla.org/gfx/printsettings-service;1");

            _printSettings = service.GetNewPrintSettingsAttribute();

            _printSettings.SetToFileNameAttribute(_pathToTempPdf);
            _printSettings.SetPrintToFileAttribute(true);
            _printSettings.SetPrintSilentAttribute(true);             //don't show a printer settings dialog
            _printSettings.SetShowPrintProgressAttribute(false);

            if (_conversionOrder.PageHeightInMillimeters > 0)
            {
                _printSettings.SetPaperHeightAttribute(_conversionOrder.PageHeightInMillimeters);
                _printSettings.SetPaperWidthAttribute(_conversionOrder.PageWidthInMillimeters);
                _printSettings.SetPaperSizeUnitAttribute(1);                 //0=in, >0 = mm
            }
            else
            {
                //doesn't actually work.  Probably a problem in the geckofx wrapper. Meanwhile we just look it up from our small list
                //printSettings.SetPaperNameAttribute(_conversionOrder.PageSizeName);

                var          size = GetPaperSize(_conversionOrder.PageSizeName);
                const double inchesPerMillimeter = 0.0393701;                   // (or more precisely, 0.0393700787402)
                _printSettings.SetPaperHeightAttribute(size.HeightInMillimeters * inchesPerMillimeter);
                _printSettings.SetPaperWidthAttribute(size.WidthInMillimeters * inchesPerMillimeter);
            }

            // BL-2346: On Linux the margins were not being set correctly due to the "unwritable margins"
            //          which were defaulting to 0.25 inches.
            _printSettings.SetUnwriteableMarginTopAttribute(0d);
            _printSettings.SetUnwriteableMarginBottomAttribute(0d);
            _printSettings.SetUnwriteableMarginLeftAttribute(0d);
            _printSettings.SetUnwriteableMarginRightAttribute(0d);

            //this seems to be in inches, and doesn't have a unit-setter (unlike the paper size ones)
            const double kMillimetersPerInch = 25.4;             // (or more precisely, 25.3999999999726)

            _printSettings.SetMarginTopAttribute(_conversionOrder.TopMarginInMillimeters / kMillimetersPerInch);
            _printSettings.SetMarginBottomAttribute(_conversionOrder.BottomMarginInMillimeters / kMillimetersPerInch);
            _printSettings.SetMarginLeftAttribute(_conversionOrder.LeftMarginInMillimeters / kMillimetersPerInch);
            _printSettings.SetMarginRightAttribute(_conversionOrder.RightMarginInMillimeters / kMillimetersPerInch);

            _printSettings.SetOrientationAttribute(_conversionOrder.Landscape ? 1 : 0);
            _printSettings.SetHeaderStrCenterAttribute("");
            _printSettings.SetHeaderStrLeftAttribute("");
            _printSettings.SetHeaderStrRightAttribute("");
            _printSettings.SetFooterStrRightAttribute("");
            _printSettings.SetFooterStrLeftAttribute("");
            _printSettings.SetFooterStrCenterAttribute("");

            _printSettings.SetPrintBGColorsAttribute(true);
            _printSettings.SetPrintBGImagesAttribute(true);

            _printSettings.SetShrinkToFitAttribute(false);


            //TODO: doesn't seem to do anything. Probably a problem in the geckofx wrapper
            //printSettings.SetScalingAttribute(_conversionOrder.Zoom);

            _printSettings.SetOutputFormatAttribute(2);             // 2 == kOutputFormatPDF

            Status = "Making PDF..";
            if (_conversionOrder.FirstPageToPrint > 0)
            {
                _printSettings.SetPrintRangeAttribute(1);                       // print only a range of pages
                if (_conversionOrder.LastPageToPrint < _conversionOrder.FirstPageToPrint)
                {
                    _conversionOrder.LastPageToPrint = _conversionOrder.FirstPageToPrint;
                }
                _printSettings.SetStartPageRangeAttribute(_conversionOrder.FirstPageToPrint);
                _printSettings.SetEndPageRangeAttribute(_conversionOrder.LastPageToPrint);
            }
            else if (_conversionOrder.ReduceMemoryUse)
            {
                _printSettings.SetPrintRangeAttribute(1);                       // print a range of pages
                _printSettings.SetStartPageRangeAttribute(1);
                _printSettings.SetEndPageRangeAttribute(1);
                _currentPage = 1;
                _currentFile = String.Format("{0}-page{1:000}", _pathToTempPdf, _currentPage);
                _printSettings.SetToFileNameAttribute(_currentFile);
                _beginPages = DateTime.Now;
                Status      = "Making Page 1 of PDF...";
            }

            _startMakingPdf = DateTime.Now;
            _print.Print(_printSettings, this);
            _checkForPdfFinishedTimer.Enabled = true;
        }