Пример #1
0
        public static void Pdf()
        {
            // By url
            HtmlToPdfHelper.Run("C:\\WkhtmlForNetCore\\index.py", "https://google.com", "google", "pdfFolder");

            // By file
            HtmlToPdfHelper.Run("C:\\WkhtmlForNetCore\\index.py", "../pdfContent.html", "pdfContent", "pdfFolder");

            // By string
            HtmlToPdfHelper.Run("C:\\WkhtmlForNetCore\\index.py", "<h1>test testov</h1>", "content", "pdfFolder");
        }
Пример #2
0
        public static byte[] ToPdfByteArrayUsingTemplate(this string html, string baseMailTemplateLocationPath)
        {
            byte[]? result = null;
            using (MemoryStream ms = new())
            {
                var styles       = FileHelper.GetFileAsString(baseMailTemplateLocationPath, BaseMailTemplateFileNameEnum.Style.GetDescription());
                var htmlHead     = FileHelper.GetFileAsString(baseMailTemplateLocationPath, BaseMailTemplateFileNameEnum.Head.GetDescription());
                var htmlTemplate = FileHelper.GetFileAsString(baseMailTemplateLocationPath, BaseMailTemplateFileNameEnum.HtmlTemplate.GetDescription());

                htmlTemplate = htmlTemplate.Replace("[HeadContent]", htmlHead).Replace("[BodyContent]", $"{styles}{html}");

                HtmlToPdf   converter = HtmlToPdfHelper.CreateHtmlToPdf(baseMailTemplateLocationPath);
                PdfDocument pdf       = converter.ConvertHtmlString(htmlTemplate);

                pdf.Save(ms);
                result = ms.ToArray();
                pdf.Close();
            }
            return(result);
        }
Пример #3
0
        static void Main(string[] args)
        {
            var content = File.ReadAllText("../../tmp.html");
            var helper  = new HtmlToPdfHelper(content);

            helper.BeginRequest();

            //launch pdf file
            helper.OpenPdf();

            //save to desktop
            var data = helper.GetPdf();
            var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), String.Format("{0}.pdf", Guid.NewGuid()));

            File.WriteAllBytes(file, data);

            //browse in browser
            helper.BrowsePdf();

            Console.ReadKey();

            helper.EndRequest();
        }
Пример #4
0
        public HttpResponse(HttpRequest request)
        {
            this.request = request;
            const string PDF_IDENT = "-pdf";

            try
            {
                if (this.request.Path == @"")
                {
                    if (HttpController.Primary == null)
                    {
                        throw new ApplicationException("Primary Controller Not Defined");
                    }
                    if (HttpController.Primary.PrimaryAction == null)
                    {
                        throw new ApplicationException("Primary Action Not Defined");
                    }

                    this.content = HttpController.RequestPrimary(this.request);
                }
                else if (this.request.Path.ToLower().EndsWith(PDF_IDENT, StringComparison.CurrentCulture))
                {
                    string url = "http://localhost:" + request.Server.Port.ToString() + this.request.FullUrl;

                    url = url.Replace(this.request.Path, StringHelper.TrimEnd(this.request.Path, PDF_IDENT.Length));

                    this.content = new HttpContent(HtmlToPdfHelper.ReadPDF(url), "application/pdf");
                }
                else if (this.request.IsWebSocket && HttpWebSocket.WebSocketControllerExists(this.request))
                {
                    var type = HttpWebSocket.GetWebSocketController(this.request);
                    this.request.WebSocket = (HttpWebSocket)Activator.CreateInstance(type, this.request);
                }
                else if (HttpController.ActionExists(this.request))
                {
                    this.content = HttpController.RequestAction(this.request);
                }
                else
                {
                    this.content = HttpContent.Read(this.request.Server, this.request.Path);
                }

                if (this.content is HttpErrorContent)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR);
                }
                else if (this.request.IsWebSocket)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SWITCHING_PROTOCOLS);
                }
                else if (this.content.ETag == this.request.ETag && !this.content.ParsingRequired)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.NOT_MODIFIED);
                }
                else
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.OK);
                }
            }
            catch (FileNotFoundException ex)
            {
                Logger.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.NOTFOUND);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.SERVERERROR);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
        }