Ejemplo n.º 1
0
        public IActionResult Pdf(string url = "http://news.baidu.com")
        {
            var pdf = new UrlAsPdf(url);

            //pdf.SaveOnServerPath = Path.Combine("wwwroot/files/image", DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");
            return(pdf);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// save image to server and return savePath not image
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task <object> PdfFile(string url = "http://news.baidu.com")
        {
            var pdf  = new UrlAsPdf(url);
            var path = Path.Combine("wwwroot/files/pdf", DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");

            pdf.SaveOnServerPath = path;
            await pdf.BuildFile(url);

            return(new { FileName = path });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// return pdf without saving to server
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public IActionResult Pdf(string url = "http://news.baidu.com")
        {
            // way 1:
            //ViewData["Message"] = "Your application description page.";
            //var model = new TestModel { Name = "Giorgio" };
            //var pdf = new ViewAsPdf("About", model);
            //return pdf;

            //way 2:
            var pdf = new UrlAsPdf(url);

            pdf.SaveOnServerPath = Path.Combine("wwwroot/files/pdf", DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");
            return(pdf);
        }
Ejemplo n.º 4
0
        public void Can_build_the_pdf_binary()
        {
            var localPath       = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory);
            var solutionDir     = localPath.Parent.Parent.Parent.FullName;
            var wkhtmltopdfPath = Path.Combine(solutionDir, "Rotativa", "Rotativa");
            var actionResult    = new UrlAsPdf("https://github.com/webgio/Rotativa")
            {
                WkhtmltopdfPath = wkhtmltopdfPath
            };
            var builder    = new TestControllerBuilder();
            var controller = new HomeController();

            builder.InitializeController(controller);
            var pdfBinary = actionResult.BuildPdf(controller.ControllerContext);
            var pdfTester = new PdfTester();

            pdfTester.LoadPdf(pdfBinary);
            pdfTester.PdfIsValid.Should().Be.True();
            pdfTester.PdfContains("Rotativa").Should().Be.True();
        }
Ejemplo n.º 5
0
        /// <summary>第五種透過BuildPdf方法再Server端產生PDF </summary>
        /// <returns></returns>
        public JsonResult LocalPDFGen()
        {
            //透過URL位置產生PDF
            UrlAsPdf GenUrlPDF = new UrlAsPdf("https://tw.news.yahoo.com/sports/");

            //設定檔案名稱
            GenUrlPDF.FileName = "dotblogs_url_test.pdf";
            //設定Margin
            GenUrlPDF.PageMargins = new Margins()
            {
                Top = 10, Bottom = 10, Left = 10, Right = 10
            };
            //指定大小預設為A4
            GenUrlPDF.PageSize = Size.A4;

            //BuildPdf此方法提供使用者再SEVER端產出PDF
            //需綁定再System.Web.Mvc.ControllerContext之下
            byte[] GenPDFinSever = GenUrlPDF.BuildPdf(this.ControllerContext);
            //存入Server端的位置
            string SavePath = GenUrlPDF.WkhtmltopdfPath + @"\" + GenUrlPDF.FileName;

            //產生PDF檔
            System.IO.File.WriteAllBytes(SavePath, GenPDFinSever);

            RtvMsg Rtv = new RtvMsg();

            if (System.IO.File.Exists(SavePath))
            {
                string UrlPath = @"http://" + Request.Url.Authority + "/Rotativa/" + GenUrlPDF.FileName;
                Rtv.Path    = UrlPath;
                Rtv.Message = "已產生檔案";
            }
            else
            {
                Rtv.Path    = "";
                Rtv.Message = "檔案未產生";
            }


            return(Json(Rtv, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 6
0
        /// <summary> 第四種透過網址產生PDF</summary>
        /// <returns></returns>
        public ActionResult UrlAsPDF()
        {
            //透過URL位置產生PDF
            UrlAsPdf GenUrlPDF = new UrlAsPdf("https://tw.news.yahoo.com/sports/");

            //設定檔案名稱
            GenUrlPDF.FileName = "dotblogs_url_test.pdf";
            //設定Margin
            GenUrlPDF.PageMargins = new Margins()
            {
                Top = 10, Bottom = 10, Left = 10, Right = 10
            };
            //指定大小預設為A4
            GenUrlPDF.PageSize = Size.A4;

            ////BuildPdf此方法提供使用者再SEVER端產出PDF
            //byte[] GenPDFinSever = GenUrlPDF.BuildPdf(this.ControllerContext);
            ////存入Server端的位置
            //System.IO.File.WriteAllBytes(@"D:\hello.pdf", GenPDFinSever);

            return(GenUrlPDF);
        }
        //
        // GET: /generate-pdf?url={url}&filename={filename?}
        public ActionResult GeneratePdf()
        {
            var url = Request.QueryString["url"];

            if (string.IsNullOrEmpty(url))
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, "Must specify a URL query parameter");
            }

            // Verify that it is a valid URI
            var dummy = new Uri(url);

            var filename = Request.QueryString["filename"];

            var view = new UrlAsPdf(url);

            if (!string.IsNullOrEmpty(filename))
            {
                view.FileName = filename;
            }
            return(view);
        }
Ejemplo n.º 8
0
        public ActionResult Generate(string id)
        {
            Guid requestedGuid = Guid.Empty;

            if (!Guid.TryParse(id, out requestedGuid))
            {
                return(Content("Guid Not Formatted properly."));
            }

            var item = Sitecore.Context.Database.GetItem(new Sitecore.Data.ID(id));

            if (item == null)
            {
                return(Content("Invalid ID"));
            }

            var itemUrl = item.GetFullyQualifiedUrl();

            var result = new UrlAsPdf(itemUrl);

            //Set Options
            if (!string.IsNullOrEmpty(Request.QueryString["name"]))
            {
                result.FileName = item.Name + ".pdf";
            }

            if (!string.IsNullOrEmpty(Request.QueryString["landscape"]))
            {
                result.PageOrientation = Rotativa.Options.Orientation.Landscape;
            }

            string requestedSize = Request.QueryString["size"];

            if (!string.IsNullOrEmpty(requestedSize))
            {
                Rotativa.Options.Size size = Rotativa.Options.Size.Letter;
                if (Enum.TryParse <Rotativa.Options.Size>(requestedSize, out size))
                {
                    result.PageSize = size;
                }
            }

            string pageWidth = Request.QueryString["width"];

            if (!string.IsNullOrEmpty(pageWidth))
            {
                double width = 0;
                if (double.TryParse(pageWidth, out width))
                {
                    result.PageWidth = width;
                }
            }
            string pageHeight = Request.QueryString["height"];

            if (!string.IsNullOrEmpty(pageHeight))
            {
                double height = 0;
                if (double.TryParse(pageHeight, out height))
                {
                    result.PageHeight = height;
                }
            }

            if (!string.IsNullOrEmpty(Request.QueryString["nomargin"]))
            {
                result.PageMargins.Left   = 0;
                result.PageMargins.Right  = 0;
                result.PageMargins.Top    = 0;
                result.PageMargins.Bottom = 0;
            }

            if (!string.IsNullOrEmpty(Request.QueryString["passauth"]))
            {
                //pass any existing cookies and auth tokens
                result.Cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k].Value);
                result.FormsAuthenticationCookieName = System.Web.Security.FormsAuthentication.FormsCookieName;
            }

            string disableSmartShrinking = Request.QueryString["dss"];

            if (!string.IsNullOrEmpty(disableSmartShrinking))
            {
                result.CustomSwitches  = "--load-error-handling ignore";
                result.CustomSwitches += " --disable-smart-shrinking";
            }

            //string saveLocal = Request.QueryString["sl"];
            //if (!string.IsNullOrEmpty(saveLocal))
            //{
            //    result.SaveOnServerPath = @"C:\inetpub\wwwroot\rms\Website\App_Data\tempPDFs";
            //}

            //var binary = result.BuildPdf(this.ControllerContext);
            // return File(binary, "application/pdf", result.FileName);
            return(result);
        }
        public ActionResult Generate(string id)
        {
            Guid requestedGuid = Guid.Empty;

            if (!Guid.TryParse(id, out requestedGuid))
            {
                return(Content("Guid Not Formatted properly."));
            }

            var item = Sitecore.Context.Database.GetItem(new Sitecore.Data.ID(id));

            if (item == null)
            {
                return(Content("Invalid ID"));
            }

            var itemUrl = item.GetFullyQualifiedUrl();

            var result = new UrlAsPdf(itemUrl);

            //Set Options
            if (!string.IsNullOrEmpty(Request.QueryString["name"]))
            {
                result.FileName = item.Name + ".pdf";
            }

            if (!string.IsNullOrEmpty(Request.QueryString["landscape"]))
            {
                result.PageOrientation = Rotativa.Options.Orientation.Landscape;
            }

            string requestedSize = Request.QueryString["size"];

            if (!string.IsNullOrEmpty(requestedSize))
            {
                Rotativa.Options.Size size = Rotativa.Options.Size.Letter;
                if (Enum.TryParse <Rotativa.Options.Size>(requestedSize, out size))
                {
                    result.PageSize = size;
                }
            }

            string pageWidth = Request.QueryString["width"];

            if (!string.IsNullOrEmpty(pageWidth))
            {
                double width = 0;
                if (double.TryParse(pageWidth, out width))
                {
                    result.PageWidth = width;
                }
            }
            string pageHeight = Request.QueryString["height"];

            if (!string.IsNullOrEmpty(pageHeight))
            {
                double height = 0;
                if (double.TryParse(pageHeight, out height))
                {
                    result.PageHeight = height;
                }
            }

            if (!string.IsNullOrEmpty(Request.QueryString["nomargin"]))
            {
                result.PageMargins.Left   = 0;
                result.PageMargins.Right  = 0;
                result.PageMargins.Top    = 0;
                result.PageMargins.Bottom = 0;
            }

            if (string.IsNullOrEmpty(Request.QueryString["ignoreCookies"]))
            {
                Dictionary <string, string> cookieCollection = new Dictionary <string, string>();
                foreach (var key in Request.Cookies.AllKeys)
                {
                    if (!cookieCollection.ContainsKey(key))
                    {
                        cookieCollection.Add(key, Request.Cookies.Get(key).Value);
                    }
                }

                //pass any existing cookies and auth tokens
                result.Cookies = cookieCollection;
                result.FormsAuthenticationCookieName = System.Web.Security.FormsAuthentication.FormsCookieName;
            }

            string disableSmartShrinking = Request.QueryString["dss"];

            if (!string.IsNullOrEmpty(disableSmartShrinking))
            {
                result.CustomSwitches  = "--load-error-handling ignore";
                result.CustomSwitches += " --disable-smart-shrinking";
            }

            return(result);
        }