Exemple #1
0
        public void FromPaperKindShouldThrowExceptionWhenPaperKindUnknown()
        {
            // Arrange
            // ReSharper disable once AssignmentIsFullyDiscarded
            Action action = () => _ = PechkinPaperSize.FromPaperKind(PaperKind.Custom);

            // Act & Assert
            action.Should().Throw <ArgumentOutOfRangeException>();
        }
Exemple #2
0
        public PaperSizeInstance(ObjectInstance prototype, PechkinPaperSize pechkinPaperSize)
            : this(prototype)
        {
            if (pechkinPaperSize == null)
            {
                throw new ArgumentNullException("pechkinPaperSize");
            }

            m_pechkinPaperSize = pechkinPaperSize;
        }
Exemple #3
0
        public void ImplicitCastShouldThrowExceptionWhenPaperKindUnknown()
        {
            // Arrange
            Action action = () =>
            {
#pragma warning disable S1481 // Unused local variables should be removed
#pragma warning disable 219
                PechkinPaperSize pps = PaperKind.Custom;
#pragma warning restore 219
#pragma warning restore S1481 // Unused local variables should be removed
            };

            // Act & Assert
            action.Should().Throw <ArgumentOutOfRangeException>();
        }
Exemple #4
0
        public void ImplicitCastShouldReturnNonEmptyValueWhenPaperKindConvertible()
        {
            // Arrange
            var paperKinds =
                Enum.GetValues(typeof(PaperKind))
                .Cast <PaperKind>()
                .Where(pk => pk != PaperKind.Custom);

            // Act & Assert
            using (new AssertionScope())
            {
                foreach (var paperKind in paperKinds)
                {
                    PechkinPaperSize pechkinPaperSize = paperKind;
                    pechkinPaperSize.Should().NotBeNull();
                    pechkinPaperSize.Width.Should().NotBeNullOrWhiteSpace();
                    pechkinPaperSize.Height.Should().NotBeNullOrWhiteSpace();
                }
            }
        }
        public byte[] Convert(string htmlContent, PechkinPaperSize paperSize)
        {
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = paperSize,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        HtmlContent = htmlContent,
                        WebSettings ={ DefaultEncoding                   = "utf-8" }
                    }
                }
            };

            return(_pdfConverter.Convert(doc));
        }
Exemple #6
0
        public static void GeneratePdf(IConverter _pdfConverter, IContent content)
        {
            try
            {
                if (content != null)
                {
                    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
                    var pdfpage       = umbracoHelper.TypedContent(content.Id);

                    if (pdfpage != null)
                    {
                        // decide if to publish or not
                        var generatePDF = content.GetValue <bool>("generatePDF");

                        if (generatePDF)
                        {
                            // now set the properties
                            var pdfTitle  = content.GetValue <string>("pDFDocumentTitle");
                            var pdfOutput = content.GetValue <string>("pDFDocumentPath");
                            var pdfUrl    = content.GetValue <string>("pDFUrl");

                            var pdfRenderDelay = 10000;
                            LogHelper.Info(typeof(PagePdf), "PDF Render delay: " + pdfRenderDelay);

                            var customPaperSize = new PechkinPaperSize("208mm", "297mm");
                            LogHelper.Info(typeof(PagePdf), "Generating PDF for url: " + pdfUrl);
                            var document = new HtmlToPdfDocument
                            {
                                GlobalSettings =
                                {
                                    ProduceOutline = false,
                                    DocumentTitle  = pdfTitle,
                                    //PaperSize = PaperKind.A4,

                                    PaperSize = customPaperSize,
                                    Margins   =
                                    {
                                        All  =               0,
                                        Unit = Unit.Millimeters
                                    }
                                },
                                Objects =
                                {
                                    new ObjectSettings {
                                        PageUrl      = pdfUrl,
                                        LoadSettings = new LoadSettings
                                        {
                                            RenderDelay     = pdfRenderDelay,
                                            DebugJavascript = false,
                                            StopSlowScript  = false,
                                        },
                                    },
                                }
                            };

                            byte[] result = _pdfConverter.Convert(document);
                            System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(pdfOutput), result);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(CorrespondentPdf), "Error: Unable to generate Correspondents PDF", ex);
            }
            return;
        }