HTMLページをPDFファイルにエクスポートしたいASP.NET MVC4アプリケーションがあります。このコードを使用すると、正常に動作します:
このコードは、html ページをオンライン PDF に変換します。ファイルを直接ダウンロードしたいと思います。
このコードを変更してこの結果を得るにはどうすればよいですか?
HTMLページをPDFファイルにエクスポートしたいASP.NET MVC4アプリケーションがあります。このコードを使用すると、正常に動作します:
このコードは、html ページをオンライン PDF に変換します。ファイルを直接ダウンロードしたいと思います。
このコードを変更してこの結果を得るにはどうすればよいですか?
FileContentResult の場合:
protected FileContentResult ViewPdf(string pageTitle, string viewName, object model)
{
// Render the view html to a string.
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
// Let the html be rendered into a PDF document through iTextSharp.
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
// Return the PDF as a binary stream to the client.
return File(buffer, "application/pdf","file.pdf");
}
添付ファイルとして作成し、結果を返すときにファイル名を付けます。
protected ActionResult ViewPdf(string pageTitle, string viewName, object model)
{
// Render the view html to a string.
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
// Let the html be rendered into a PDF document through iTextSharp.
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
// Return the PDF as a binary stream to the client.
return File(buffer, "application/pdf", "myfile.pdf");
}
ファイルが添付ファイルとして表示され、[名前を付けて保存] ダイアログがポップアップ表示されるのは、次の行です。
return File(buffer, "application/pdf", "myfile.pdf");
使用する:
これはVB.NET用です(以下のC#)
Public Function PDF() As FileResult
Return File("../PDFFile.pdf", "application/pdf")
End Function
あなたのアクションメソッドで。PDFFIle はファイル名です。
C# の場合
Public FileResult PDF(){
return File("../PDFFile.pdf", "application/pdf");
}