Beispiel #1
0
        public ActionResult Print(int id)
        {
            RequestRelease printRequest = db.RequestReleases
                                        .Where(a => a.RequestID == id).SingleOrDefault();

            if (printRequest == null)
            {
                return RedirectToAction("PerjadinNotRelease");
            }
            else
            {
                MemoryStream memory = new MemoryStream();
                var PrintPageUrl = ConfigurationManager.AppSettings["PrintRequest"];
                PdfDocument document = new PdfDocument() { Url = PrintPageUrl + id };
                PdfOutput output = new PdfOutput() { OutputStream = memory };

                PdfConvert.ConvertHtmlToPdf(document, output);
                memory.Position = 0;

                return File(memory, "application/pdf", Server.UrlEncode("E-SKPD.pdf"));
            }
        }
Beispiel #2
0
        public ActionResult Print(int id)
        {
            MemoryStream memory = new MemoryStream();
            PdfDocument document = new PdfDocument() { Url = "http://localhost:52858/Pengajuan/PrintPage/" + id };
            PdfOutput output = new PdfOutput() { OutputStream = memory };

            PdfConvert.ConvertHtmlToPdf(document, output);
            memory.Position = 0;

            return File(memory, "application/pdf", Server.UrlEncode("E-SKPD.pdf"));
        }
Beispiel #3
0
        public ActionResult PrintReport(RangeReleaseReport model)
        {
            var filename = string.Format("{0}{1}-{2}.pdf", "ReportSKPD", model.Start.ToShortDateString().ToString().Replace("/", ""), model.From.ToShortDateString().Replace("/", ""));
            IEnumerable<ReportRequest> rr = db.ReportRequests;

            if (rr == null)
            {
                return RedirectToAction("PerjadinNotRelease");
            }
            else
            {
                MemoryStream memory = new MemoryStream();
                var PrintPageUrl = ConfigurationManager.AppSettings["Report"];
                var PrintReportUrl = string.Format("{0}?start={1}&end={2}", PrintPageUrl, model.Start.ToShortDateString().ToString(), model.From.ToShortDateString().ToString());
                PdfDocument document = new PdfDocument() { Url = PrintReportUrl };
                PdfOutput output = new PdfOutput() { OutputStream = memory };

                PdfConvert.ConvertHtmlToPdf(document, output);
                memory.Position = 0;

                return File(memory, "application/pdf", Server.UrlEncode(filename));
            }
        }
Beispiel #4
0
        public ActionResult wkhtml()
        {
            MemoryStream memory = new MemoryStream();
            PdfDocument document = new PdfDocument() { Url = "http://localhost:52858/paging/Index/2?currentFilter=kadiv" };
            PdfOutput output = new PdfOutput() { OutputStream = memory };

            PdfConvert.ConvertHtmlToPdf(document, output);
            memory.Position = 0;

            return File(memory, "application/pdf", Server.UrlEncode("dsdas.pdf"));
        }
Beispiel #5
0
        public static void ConvertHtmlToPdf(PdfDocument document, PdfConvertEnvironment environment, PdfOutput woutput)
        {
            if (environment == null)
                environment = Environment;

            String outputPdfFilePath;
            bool delete;
            if (woutput.OutputFilePath != null)
            {
                outputPdfFilePath = woutput.OutputFilePath;
                delete = false;
            }
            else
            {
                outputPdfFilePath = Path.Combine(environment.TempFolderPath, String.Format("{0}.pdf", Guid.NewGuid()));
                delete = true;
            }

            if (!File.Exists(environment.WkHtmlToPdfPath))
                throw new PdfConvertException(String.Format("File '{0}' not found. Check if wkhtmltopdf application is installed.", environment.WkHtmlToPdfPath));

            ProcessStartInfo si;

            StringBuilder paramsBuilder = new StringBuilder();
            paramsBuilder.Append("--page-size A4 ");
            //paramsBuilder.Append("--redirect-delay 0 "); not available in latest version
            if (!string.IsNullOrEmpty(document.HeaderUrl))
            {
                paramsBuilder.AppendFormat("--header-html {0} ", document.HeaderUrl);
                paramsBuilder.Append("--margin-top 25 ");
                paramsBuilder.Append("--header-spacing 5 ");
            }
            if (!string.IsNullOrEmpty(document.FooterUrl))
            {
                paramsBuilder.AppendFormat("--footer-html {0} ", document.FooterUrl);
                paramsBuilder.Append("--margin-bottom 25 ");
                paramsBuilder.Append("--footer-spacing 5 ");
            }

            paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", document.Url, outputPdfFilePath);

            si = new ProcessStartInfo();
            si.CreateNoWindow = !environment.Debug;
            si.FileName = environment.WkHtmlToPdfPath;
            si.Arguments = paramsBuilder.ToString();
            si.UseShellExecute = false;
            si.RedirectStandardError = !environment.Debug;

            try
            {
                using (var process = new Process())
                {
                    process.StartInfo = si;
                    process.Start();

                    if (!process.WaitForExit(environment.Timeout))
                        throw new PdfConvertTimeoutException();

                    if (!File.Exists(outputPdfFilePath))
                    {
                        if (process.ExitCode != 0)
                        {
                            var error = si.RedirectStandardError ? process.StandardError.ReadToEnd() : String.Format("Process exited with code {0}.", process.ExitCode);
                            throw new PdfConvertException(String.Format("Html to PDF conversion of '{0}' failed. Wkhtmltopdf output: \r\n{1}", document.Url, error));
                        }

                        throw new PdfConvertException(String.Format("Html to PDF conversion of '{0}' failed. Reason: Output file '{1}' not found.", document.Url, outputPdfFilePath));
                    }

                    if (woutput.OutputStream != null)
                    {
                        using (Stream fs = new FileStream(outputPdfFilePath, FileMode.Open))
                        {
                            byte[] buffer = new byte[32 * 1024];
                            int read;

                            while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                                woutput.OutputStream.Write(buffer, 0, read);
                        }
                    }

                    if (woutput.OutputCallback != null)
                    {
                        woutput.OutputCallback(document, File.ReadAllBytes(outputPdfFilePath));
                    }
                }
            }
            finally
            {
                if (delete && File.Exists(outputPdfFilePath))
                    File.Delete(outputPdfFilePath);
            }
        }
Beispiel #6
0
 public static void ConvertHtmlToPdf(PdfDocument document, PdfOutput output)
 {
     ConvertHtmlToPdf(document, null, output);
 }