コード例 #1
0
ファイル: WkPdfService.cs プロジェクト: grandnode/grandnode2
        public async Task PrintOrdersToPdf(Stream stream, IList <Order> orders, string languageId = "", string vendorId = "")
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (orders == null)
            {
                throw new ArgumentNullException(nameof(orders));
            }

            _generatePdf.SetConvertOptions(new ConvertOptions()
            {
                PageSize    = Wkhtmltopdf.NetCore.Options.Size.A4,
                PageMargins = new Wkhtmltopdf.NetCore.Options.Margins()
                {
                    Bottom = 10, Left = 10, Right = 10, Top = 10
                },
                FooterHtml = CommonPath.WebMapPath(_orderFooter)
            });

            var html = await _viewRenderService.RenderToStringAsync <(IList <Order>, string)>(_orderTemaplate, new (orders, vendorId));

            var pdfBytes = _generatePdf.GetPDF(html);

            stream.Write(pdfBytes);
        }
コード例 #2
0
        public async Task <IActionResult> GenerateCertificate(string ticket, string firstName, string lastName)
        {
            var connection = new Connection();
            var response   = connection.IsTicketNumberValid(ticket);

            if (response.IsValid)
            {
                var assembly = Assembly.GetEntryAssembly();
                using (var stream = assembly.GetManifestResourceStream("devfestcertapi.PDF.template.html"))
                    using (var reader = new StreamReader(stream))
                    {
                        string template = await reader.ReadToEndAsync();

                        var updatedtemplate = template
                                              .Replace("{attendee}", string.Format("{0} {1}", response.FirstName, response.LastName))
                                              .Replace("{ticketNumber}", ticket);
                        var options = new ConvertOptions();
                        options.IsLowQuality       = false;
                        options.PageMargins.Bottom = 0;
                        options.PageMargins.Left   = 0;
                        options.PageMargins.Right  = 0;
                        options.PageMargins.Top    = 0;
                        options.PageOrientation    = Wkhtmltopdf.NetCore.Options.Orientation.Landscape;

                        _generatePdf.SetConvertOptions(options);
                        var pdf = _generatePdf.GetPDF(updatedtemplate);
                        Response.Headers.Add("Content-Disposition", "inline; filename=GDGPH2020Certificate.pdf");
                        return(File(pdf.ToArray(), "application/pdf"));
                    }
            }
            return(new BadRequestResult());
        }
コード例 #3
0
        public async Task <byte[]> GenerateExamplePdf(string userName)
        {
            var model = new CertificateModel
            {
                Name           = userName,
                Date           = DateTime.Now.ToShortDateString(),
                BackgroundPath = $"{AppContext.BaseDirectory}MustacheTemplates/Certificate/certificate_bg.jpg"
            };

            var certificateContent = await ProcessTemplate
                                         (model, @"./MustacheTemplates/Certificate/ExampleCertificateTemplate.html");

            var options = new ConvertOptions
            {
                PageOrientation = Orientation.Landscape,
                PageMargins     = new Margins(0, 0, 0, 0),
            };

            _generatePdf.SetConvertOptions(options);

            var bytes = _generatePdf.GetPDF(certificateContent);

            // If you want to save file on disc
            // await File.WriteAllBytesAsync("file.pdf", bytes);

            return(bytes);
        }
コード例 #4
0
ファイル: PDFTestController.cs プロジェクト: raikay/Demo
        public IActionResult TestMarginAndPageSize()
        {
            string strHtmlTemplate = @"<!DOCTYPE html>
                        <html>
                        <head>
                        </head>
                        <body>
                            <header>
                                <h1>Zoe</h1>
                            </header>
                            <div>
                                <h2>欢迎关注Code综艺圈</h2>
                            </div>
                        </body>";
            var    options         = new ConvertOptions
            {
                PageMargins = new Wkhtmltopdf.NetCore.Options.Margins
                {
                    Bottom = 10, //下边距
                    Left   = 0,  //左边距
                    Right  = 0,  //右边距
                    Top    = 15  //上边距
                },
                PageSize = Wkhtmltopdf.NetCore.Options.Size.A5
            };

            _generatePdf.SetConvertOptions(options);
            var pdf           = _generatePdf.GetPDF(strHtmlTemplate);
            var pdfFileStream = new MemoryStream();

            pdfFileStream.Write(pdf, 0, pdf.Length);
            pdfFileStream.Position = 0;
            return(new FileStreamResult(pdfFileStream, "application/pdf"));
        }
        public async Task <IActionResult> HeaderTest()
        {
            var options = new ConvertOptions
            {
                HeaderHtml      = "http://localhost:30001/header.html",
                PageOrientation = Wkhtmltopdf.NetCore.Options.Orientation.Landscape
            };

            _generatePdf.SetConvertOptions(options);

            var data = new TestData
            {
                Text   = "This is a test",
                Number = 123456
            };

            var pdf = await _generatePdf.GetByteArray("Views/Test.cshtml", data);

            var pdfStream = new System.IO.MemoryStream();

            pdfStream.Write(pdf, 0, pdf.Length);
            pdfStream.Position = 0;
            return(new FileStreamResult(pdfStream, "application/pdf"));
        }
コード例 #6
0
        public IActionResult GetFormByteArray()
        {
            var options = new ConvertOptions
            {
                EnableForms = true
            };

            _generatePdf.SetConvertOptions(options);

            var pdf       = _generatePdf.GetPDF(formHtml);
            var pdfStream = new System.IO.MemoryStream();

            pdfStream.Write(pdf, 0, pdf.Length);
            pdfStream.Position = 0;
            return(new FileStreamResult(pdfStream, "application/pdf"));
        }
コード例 #7
0
        public async Task <IActionResult> GetPDF([FromBody] Dictionary <string, object> rawdata, string template)
        {
            // first do a mustache merge.
            var    stubble  = new StubbleBuilder().Build();
            string filename = $"Templates/{template}.mustache";

            if (System.IO.File.Exists(filename))
            {
                string format = System.IO.File.ReadAllText(filename);
                var    html   = stubble.Render(format, rawdata);

                _generatePdf.SetConvertOptions(new ConvertOptions
                {
                    PageSize    = Size.Letter,
                    PageMargins = new Margins(5, 5, 5, 5)
                });

                var pdf = await _generatePdf.GetPdfViewInHtml(html);

                return(pdf);
            }

            return(new NotFoundResult());
        }