public FileContentResult Imprimir(string registros) { string html = ""; if (registros == "TabelaAluno") { html = RenderRazorViewToString("TabelaAluno", new AlunoDal().ObterTodos().ToList()); } if (registros == "TabelaCidade") { html = RenderRazorViewToString("TabelaCidade", new CidadeDal().ObterTodos().ToList()); } if (registros == "TabelaAlunoCidade") { html = RenderRazorViewToString("TabelaAlunoCidade", new CidadeDal().ObterTodos().ToList()); } var properties = new ConverterProperties(); var baseUrl = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")); properties.SetBaseUri(baseUrl); MediaDeviceDescription med = new MediaDeviceDescription(MediaType.ALL); med.SetOrientation("landscape"); properties.SetMediaDeviceDescription(med); using (var stream = new MemoryStream()) { HtmlConverter.ConvertToPdf(html, stream, properties); return(File(stream.ToArray(), "application/pdf")); } }
public virtual void Test02() { String htmlFileName = sourceFolder + "html02.html"; String cssFileName = sourceFolder + "css02.css"; IXmlParser htmlParser = new JsoupHtmlParser(); IDocumentNode document = htmlParser.Parse(new FileStream(htmlFileName, FileMode.Open, FileAccess.Read), "UTF-8" ); CssStyleSheet css = CssStyleSheetParser.Parse(new FileStream(cssFileName, FileMode.Open, FileAccess.Read)); IElementNode element = new JsoupElementNode(((JsoupDocumentNode)document).GetDocument().GetElementsByTag("p" ).First()); MediaDeviceDescription deviceDescription1 = new MediaDeviceDescription(MediaType.PRINT); deviceDescription1.SetWidth(525); MediaDeviceDescription deviceDescription2 = new MediaDeviceDescription(MediaType.HANDHELD); deviceDescription2.SetOrientation("landscape"); IList <CssDeclaration> declarations1 = css.GetCssDeclarations(element, deviceDescription1); IList <CssDeclaration> declarations2 = css.GetCssDeclarations(element, deviceDescription2); NUnit.Framework.Assert.IsTrue(Enumerable.SequenceEqual(declarations1, declarations2)); NUnit.Framework.Assert.AreEqual(1, declarations1.Count); NUnit.Framework.Assert.AreEqual("font-weight: bold", declarations1[0].ToString()); }