YanoRyuichi.com/
Wiki
Blog
GitHub
Sandbox
開始行:
* ASP.NET MVCで利用する [#h4b04034]
** 基本方針 [#se9bc264]
- PartialViewで指定したHTML(PDFに変換する)をstringにす...
- HTMLストリングをiTextSharpに渡してPDFのバイトコードにす...
- PDFのバイトコードをコントローラーでレスポンスにする。
** コード [#xb72b48f]
*** Controller [#de1fe381]
public class HomeController : Controller
{
public ActionResult Index()
{
string result = Utils.RenderRazorViewToString(th...
var buffer = HtmlToPdf.ConvertHtmlToPdf(result);
return File(buffer, "application/pdf", "test1.pd...
}
}
*** RenderRazorViewToString [#e5675441]
public static class Utils
{
public static string RenderRazorViewToString(this Con...
{
controller.ViewData.Model = model;
using (var sw = new StringWriter()) {
var viewResult = ViewEngines.Engines.FindPart...
var viewContext = new ViewContext(controller....
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller....
return sw.GetStringBuilder().ToString();
}
}
}
*** HtmlToPdf [#z9faf5c1]
public class HtmlToPdf
{
public static byte[] ConvertHtmlToPdf(string html)
{
MemoryStream output = new MemoryStream();
Document document = new Document(PageSize.A4, 30,...
HTMLWorker worker = new HTMLWorker(document);
var provider = new Dictionary<string, object>() {
{ HTMLWorker.FONT_PROVIDER, new MyFontProvide...
};
worker.SetProviders(provider);
PdfWriter writer = PdfWriter.GetInstance(document...
writer.CloseStream = false;
document.Open();
worker.StartDocument();
TextReader reader = new StringReader(html);
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
return output.ToArray();
}
}
public class MyFontProvider : FontFactoryImp
{
public override Font GetFont(string fontname, string ...
{
if (string.IsNullOrEmpty(fontname)) {
fontname = "c:\\windows\\fonts\\msmincho.ttc,...
encoding = BaseFont.IDENTITY_H;
embedded = BaseFont.EMBEDDED;
}
return base.GetFont(fontname, encoding, embedded,...
}
}
** 参考 [#xf972c61]
- http://www.codeproject.com/Articles/66948/Rendering-PDF...
- http://forums.asp.net/t/1889126.aspx?How+to+insert+Docu...
- http://www.codeproject.com/Articles/260470/PDF-reportin...
- http://stackoverflow.com/questions/16975657/download-pd...
- http://stackoverflow.com/questions/2822843/itextsharp-h...
終了行:
* ASP.NET MVCで利用する [#h4b04034]
** 基本方針 [#se9bc264]
- PartialViewで指定したHTML(PDFに変換する)をstringにす...
- HTMLストリングをiTextSharpに渡してPDFのバイトコードにす...
- PDFのバイトコードをコントローラーでレスポンスにする。
** コード [#xb72b48f]
*** Controller [#de1fe381]
public class HomeController : Controller
{
public ActionResult Index()
{
string result = Utils.RenderRazorViewToString(th...
var buffer = HtmlToPdf.ConvertHtmlToPdf(result);
return File(buffer, "application/pdf", "test1.pd...
}
}
*** RenderRazorViewToString [#e5675441]
public static class Utils
{
public static string RenderRazorViewToString(this Con...
{
controller.ViewData.Model = model;
using (var sw = new StringWriter()) {
var viewResult = ViewEngines.Engines.FindPart...
var viewContext = new ViewContext(controller....
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller....
return sw.GetStringBuilder().ToString();
}
}
}
*** HtmlToPdf [#z9faf5c1]
public class HtmlToPdf
{
public static byte[] ConvertHtmlToPdf(string html)
{
MemoryStream output = new MemoryStream();
Document document = new Document(PageSize.A4, 30,...
HTMLWorker worker = new HTMLWorker(document);
var provider = new Dictionary<string, object>() {
{ HTMLWorker.FONT_PROVIDER, new MyFontProvide...
};
worker.SetProviders(provider);
PdfWriter writer = PdfWriter.GetInstance(document...
writer.CloseStream = false;
document.Open();
worker.StartDocument();
TextReader reader = new StringReader(html);
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
return output.ToArray();
}
}
public class MyFontProvider : FontFactoryImp
{
public override Font GetFont(string fontname, string ...
{
if (string.IsNullOrEmpty(fontname)) {
fontname = "c:\\windows\\fonts\\msmincho.ttc,...
encoding = BaseFont.IDENTITY_H;
embedded = BaseFont.EMBEDDED;
}
return base.GetFont(fontname, encoding, embedded,...
}
}
** 参考 [#xf972c61]
- http://www.codeproject.com/Articles/66948/Rendering-PDF...
- http://forums.asp.net/t/1889126.aspx?How+to+insert+Docu...
- http://www.codeproject.com/Articles/260470/PDF-reportin...
- http://stackoverflow.com/questions/16975657/download-pd...
- http://stackoverflow.com/questions/2822843/itextsharp-h...
ページ名: