web-dev-qa-db-ja.com

MVCでブラウザにPDFを返す方法は?

このiTextSharpのデモコードがあります

    Document document = new Document();
    try
    {
        PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));

        document.Open();

        document.Add(new Paragraph("Hello World"));

    }
    catch (DocumentException de)
    {
        Console.Error.WriteLine(de.Message);
    }
    catch (IOException ioe)
    {
        Console.Error.WriteLine(ioe.Message);
    }

    document.Close();

コントローラーがPDFドキュメントをブラウザーに返すようにするにはどうすればよいですか?

編集:

このコードを実行するとAcrobatが開きますが、「ファイルが破損しており、修復できませんでした」というエラーメッセージが表示されます

  public FileStreamResult pdf()
    {
        MemoryStream m = new MemoryStream();
        Document document = new Document();
        PdfWriter.GetInstance(document, m);
        document.Open();
        document.Add(new Paragraph("Hello World"));
        document.Add(new Paragraph(DateTime.Now.ToString()));
        m.Position = 0;

        return File(m, "application/pdf");
    }

これが機能しない理由はありますか?

115
Tony Borf

このコードで動作するようにしました。

using iTextSharp.text;
using iTextSharp.text.pdf;

public FileStreamResult pdf()
{
    MemoryStream workStream = new MemoryStream();
    Document document = new Document();
    PdfWriter.GetInstance(document, workStream).CloseStream = false;

    document.Open();
    document.Add(new Paragraph("Hello World"));
    document.Add(new Paragraph(DateTime.Now.ToString()));
    document.Close();

    byte[] byteInfo = workStream.ToArray();
    workStream.Write(byteInfo, 0, byteInfo.Length);
    workStream.Position = 0;

    return new FileStreamResult(workStream, "application/pdf");    
}
60
Tony Borf

FileContentResultを返します。コントローラーアクションの最後の行は次のようになります。

return File("Chap0101.pdf", "application/pdf");

このPDFを動的に生成する場合は、MemoryStreamを使用して、ファイルに保存する代わりにメモリ内にドキュメントを作成する方がよい場合があります。コードは次のようになります。

Document document = new Document();

MemoryStream stream = new MemoryStream();

try
{
    PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
    pdfWriter.CloseStream = false;

    document.Open();
    document.Add(new Paragraph("Hello World"));
}
catch (DocumentException de)
{
    Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
    Console.Error.WriteLine(ioe.Message);
}

document.Close();

stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required

return File(stream, "application/pdf", "DownloadName.pdf");
123
Geoff

以下を指定する必要があります。

Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
return new FileStreamResult(stream, "application/pdf")

ファイルをダウンロード済みではなく、ブラウザで直接開いたにする

19
Machinegon

アクションメソッドからFileResultを返し、コントローラーでFile()拡張メソッドを使用する場合、必要な処理は非常に簡単です。 File()メソッドには、ファイルのバイナリコンテンツ、ファイルへのパス、またはStreamを取得するオーバーライドがあります。

public FileResult DownloadFile()
{
    return File("path\\to\\pdf.pdf", "application/pdf");
}
16
NerdFury

私は同様の問題に遭遇し、解決策に出くわしました。 2つの投稿を使用しました。1つは stack から、ダウンロードのために戻る方法を示しています。もう1つは one はItextSharpとMVCの有効なソリューションを示しています。

public FileStreamResult About()
{
    // Set up the document and the MS to write it to and create the PDF writer instance
    MemoryStream ms = new MemoryStream();
    Document document = new Document(PageSize.A4.Rotate());
    PdfWriter writer = PdfWriter.GetInstance(document, ms);

    // Open the PDF document
    document.Open();

    // Set up fonts used in the document
    Font font_heading_1 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 19, Font.BOLD);
    Font font_body = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9);

    // Create the heading paragraph with the headig font
    Paragraph paragraph;
    paragraph = new Paragraph("Hello world!", font_heading_1);

    // Add a horizontal line below the headig text and add it to the paragraph
    iTextSharp.text.pdf.draw.VerticalPositionMark seperator = new iTextSharp.text.pdf.draw.LineSeparator();
    seperator.Offset = -6f;
    paragraph.Add(seperator);

    // Add paragraph to document
    document.Add(paragraph);

    // Close the PDF document
    document.Close();

    // Hat tip to David for his code on stackoverflow for this bit
    // https://stackoverflow.com/questions/779430/asp-net-mvc-how-to-get-view-to-generate-pdf
    byte[] file = ms.ToArray();
    MemoryStream output = new MemoryStream();
    output.Write(file, 0, file.Length);
    output.Position = 0;

    HttpContext.Response.AddHeader("content-disposition","attachment; filename=form.pdf");


    // Return the output stream
    return File(output, "application/pdf"); //new FileStreamResult(output, "application/pdf");
}
11
littlechris

カスタムクラスを作成して、コンテンツタイプを変更し、応答にファイルを追加できます。

http://haacked.com/archive/2008/05/10/writing-a-custom-file-download-action-result-for-asp.net-mvc.aspx

3
Chris Kooken

私はこの質問が古いことを知っていますが、似たようなものを見つけることができなかったので、これを共有すると思いました。

ビュー/モデルを通常のRazorを使用して作成し、それらをPdfsとしてレンダリングしたかった。

このようにして、iTextSharpを使用してドキュメントをレイアウトする方法を考えるのではなく、標準のHTML出力を使用してpdfプレゼンテーションを制御できました。

プロジェクトとソースコードは、nugetのインストール手順とともにここから入手できます。

https://github.com/andyhutch77/MvcRazorToPdf

Install-Package MvcRazorToPdf
3
hutchonoid

通常、Response.Flushに続いてResponse.Closeを実行しますが、何らかの理由でiTextSharpライブラリがこれを好まないようです。データは処理されず、AdobeはPDFが破損していると判断します。 Response.Close関数を省略して、結果が改善されているかどうかを確認します。

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-disposition", "attachment; filename=file.pdf"); // open in a new window
Response.OutputStream.Write(outStream.GetBuffer(), 0, outStream.GetBuffer().Length);
Response.Flush();

// For some reason, if we close the Response stream, the PDF doesn't make it through
//Response.Close();
2
JML
HttpContext.Response.AddHeader("content-disposition","attachment; filename=form.pdf");

ファイル名が動的に生成される場合、ここでファイル名を定義する方法は、ここでGUIDを介して生成されます。

2
S.J.Lee

FileStreamResultは確かに機能します。しかし Microsoft Docs を見ると、別の派生クラス FileContentResult を持つActionResult -> FileResultを継承しています。 「バイナリファイルの内容を応答に送信します」。したがって、すでにbyte []がある場合は、代わりにFileContentResultを使用する必要があります。

public ActionResult DisplayPDF()
{
    byte[] byteArray = GetPdfFromWhatever();

    return new FileContentResult(byteArray, "application/pdf");
}
1
Weihui Guo

ポップアップまたはブラウザでPDFを表示するためにvar-binaryデータをDBから返す場合、次のコードに従うことを意味します-

ページを表示:

@using (Html.BeginForm("DisplayPDF", "Scan", FormMethod.Post))
    {
        <a href="javascript:;" onclick="document.forms[0].submit();">View PDF</a>
    }

スキャンコントローラー:

public ActionResult DisplayPDF()
        {
            byte[] byteArray = GetPdfFromDB(4);
            MemoryStream pdfStream = new MemoryStream();
            pdfStream.Write(byteArray, 0, byteArray.Length);
            pdfStream.Position = 0;
            return new FileStreamResult(pdfStream, "application/pdf");
        }

        private byte[] GetPdfFromDB(int id)
        {
            #region
            byte[] bytes = { };
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT Scan_Pdf_File FROM PWF_InvoiceMain WHERE InvoiceID=@Id and Enabled = 1";
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        if (sdr.HasRows == true)
                        {
                            sdr.Read();
                            bytes = (byte[])sdr["Scan_Pdf_File"];
                        }
                    }
                    con.Close();
                }
            }

            return bytes;
            #endregion
        }
1
ethiraj