public Task<HttpResponseMessage> PDF(int id, [FromUri]string empresa, string cliente, string inmueble, string local) { log.DebugFormat("Empresa: {0}, Cliente: {1}, Inmueble: {2}, Local: {3}", empresa, cliente, inmueble, local); if (id == 1) { string jsonSerial = serializer.Serialize(Queryable.AsQueryable<spPANGEAEdoCta_Result>(db.spPANGEAEdoCta(empresa, cliente, inmueble, local)).ToList()); using (Stream s = GenerateStreamFromString(jsonSerial)) { var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(s) }; response.Content = new ByteArrayContent(((MemoryStream)s).ToArray()); response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); return Task.FromResult(response); } } else { string pangeaWebApiCookie = null; CookieHeaderValue cookie = Request.Headers.GetCookies(supraterraCookie).FirstOrDefault(); var response = new HttpResponseMessage(HttpStatusCode.Forbidden); if (cookie != null) { pangeaWebApiCookie = cookie[supraterraCookie].Value; } else { return Task.FromResult(response); } var encoding = Encoding.GetEncoding("iso-8859-1"); var credentials = encoding.GetString(Convert.FromBase64String(pangeaWebApiCookie)); log.DebugFormat("Credenciales: {0}", credentials); int separator = credentials.IndexOf(':'); string name = credentials.Substring(0, separator); string password = credentials.Substring(separator + 1); if (CheckPassword(name, password)) { LocalReport report = new LocalReport(); //var assembly = Assembly.LoadFrom("PangeaWebApi.dll"); //var stream = assembly.GetManifestResourceStream("PangeaWebApi.Reportes.EdoCta.rdlc"); string reportPath = HttpContext.Current.Server.MapPath("~/Reportes/EdoCta.rdlc"); log.Debug(reportPath); //report.LoadReportDefinition(str) report.ReportPath = reportPath; ReportDataSource rds = new ReportDataSource(); rds.Name = "EdoCtaDataSet";//This refers to the dataset name in the RDLC file spPANGEAEdoCtaTableAdapter da = new spPANGEAEdoCtaTableAdapter(); rds.Value = da.GetData(empresa, cliente, inmueble, local); report.DataSources.Add(rds); ReportParameter[] parametros = new ReportParameter[1]; parametros[0] = new ReportParameter("Empresa", empresa); report.SetParameters(parametros); //Byte[] mybytes = report.Render("WORD"); Byte[] mybytes = report.Render("PDF");// for exporting to PDF //using (FileStream fs = File.Create(@"D:\SalSlip.doc")) //{ // fs.Write(mybytes, 0, mybytes.Length); //} Stream s = new MemoryStream(); //pdf.Save(s, false); response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ByteArrayContent(mybytes); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = "Estado de Cuenta " + cliente + "-" + local + ".pdf"; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); } return Task.FromResult(response); } }
public Task<HttpResponseMessage> PDF([FromUri]string empresa, [FromUri]string cliente, [FromUri]string inmueble, [FromUri]string local) { log.DebugFormat("Empresa: {0}, Cliente: {1}, Inmueble: {2}, Local: {3}", empresa, cliente, inmueble, local); string pangeaWebApiCookie = null; CookieHeaderValue cookie = Request.Headers.GetCookies(supraterraCookie).FirstOrDefault(); var response = new HttpResponseMessage(HttpStatusCode.Forbidden); if (cookie != null) { pangeaWebApiCookie = cookie[supraterraCookie].Value; } else { return Task.FromResult(response); } var encoding = Encoding.GetEncoding("iso-8859-1"); var credentials = encoding.GetString(Convert.FromBase64String(pangeaWebApiCookie)); log.DebugFormat("Credenciales: {0}", credentials); int separator = credentials.IndexOf(':'); string name = credentials.Substring(0, separator); string password = credentials.Substring(separator + 1); if (CheckPassword(name, password)) { LocalReport report = new LocalReport(); string reportPath = HttpContext.Current.Server.MapPath("~/Reports/EdoCta.rdlc"); log.Debug(reportPath); report.ReportPath = reportPath; ReportDataSource rds = new ReportDataSource(); rds.Name = "EdoCtaDataSet"; spPANGEAEdoCtaTableAdapter da = new spPANGEAEdoCtaTableAdapter(); log.Debug("--->0"); rds.Value = da.GetData(empresa, cliente, inmueble, local); log.Debug("--->0"); report.DataSources.Add(rds); log.Debug("--->0"); ReportParameter[] parametros = new ReportParameter[1]; log.Debug("--->0"); parametros[0] = new ReportParameter("Empresa", empresa); report.SetParameters(parametros); Byte[] mybytes = report.Render("PDF");// for exporting to PDF /// FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~/generados/output.pdf"), FileMode.Create); fs.Write(mybytes, 0, mybytes.Length); fs.Close(); ////// MemoryStream outPDF = new MemoryStream(); PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("~/generados/output.pdf")); int i = 0; int p = 0; int n = reader.NumberOfPages; Rectangle psize = reader.GetPageSize(1); Document document = new Document(psize); PdfWriter writer = PdfWriter.GetInstance(document, outPDF); document.Open(); PdfContentByte cb = writer.DirectContent; float width = psize.Width; float height = psize.Height; //Add Page to new document while (i < n) { document.NewPage(); p++; i++; PdfImportedPage page1 = writer.GetImportedPage(reader, i); cb.AddTemplate(page1, 0, 0); } //Attach javascript to the document //PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer); //writer.AddJavaScript(jAction); PdfAction action = new PdfAction(PdfAction.PRINTDIALOG); writer.SetOpenAction(action); document.Close(); reader.Close(); response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ByteArrayContent(outPDF.ToArray()); var userAgent = Request.Headers.UserAgent.ToString(); if (userAgent.Contains("Chrome")) { response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline"); } else { response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); } response.Content.Headers.ContentDisposition.FileName = "Estado de Cuenta " + cliente + "-" + local + ".pdf"; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); return Task.FromResult(response); } else { return Task.FromResult(response); } }