2016-09-30 6 views
0

ハンドラを使用してpdfを保存する際に問題があります。 aspxページのバックエンドから直接使用している場合、csファイルからは正常に保存されています。しかし、私がashxハンドラからコードを実行しようとすると何も起こりません。 私のコード:javascript関数としてitextsharpを使用してashxハンドラからPDFを保存します。

<a target="_blank" href="/exportPDF.ashx">Export PDF</a> 

:リンクとして

public void ProcessRequest(HttpContext context) 
    { 
HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.ContentType = "application/pdf"; 
      HttpContext.Current.Response.AddHeader("content-     disposition", "attachment;filename=john.pdf"); 
                       HttpContext.Current.Response.Cache.SetCacheability         (HttpCacheability.NoCache); 
      StringWriter stringWriter = new StringWriter(); 
      HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); 

      string imagepath = context.Server.MapPath(@"~/img/logo3.png"); 
      Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f); 
      HTMLWorker htmlparser = new HTMLWorker(Doc); 
      PdfWriter pdfwriter = PdfWriter.GetInstance(Doc, HttpContext.Current.Response.OutputStream); 
      Doc.Open(); 
      iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath); 
      image.ScalePercent(106f, 90f); 
      Doc.Add(image); 
      AddPDf(pdfwriter, Doc); 
      OnEndPage(pdfwriter, Doc); 
      Doc.Close(); 
      HttpContext.Current.Response.End(); 
      } 

public void AddPDf(PdfWriter writer, Document document) 
{ 
PdfPTable table = new PdfPTable(3); 
table.TotalWidth = 400f; 
//fix the absolute width of the table 
table.LockedWidth = true; 
//relative col widths in proportions - 1/3 and 2/3 
float[] widths = new float[] { 2f, 4f, 6f }; 
table.SetWidths(widths); 
table.HorizontalAlignment = 0; 
//leave a gap before and after the table 
table.SpacingBefore = 20f; 
    table.SpacingAfter = 30f; 
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns")); 
cell.Colspan = 3; 
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right 
table.AddCell(cell); 
table.AddCell("Col 1 Row 1"); 
    table.AddCell("Col 2 Row 1"); 
    table.AddCell("Col 3 Row 1"); 
    table.AddCell("Col 1 Row 2"); 
    table.AddCell("Col 2 Row 2"); 
    table.AddCell("Col 3 Row 2"); 
document.Open(); 
document.Add(table); 
    } 
    public void OnEndPage(PdfWriter writer, Document document) 
    { 


     var content = writer.DirectContent; 
     var pageBorderRect = new Rectangle(document.PageSize); 

     pageBorderRect.Left += document.LeftMargin; 
     pageBorderRect.Right -= document.RightMargin; 
     pageBorderRect.Top -= document.TopMargin; 
     pageBorderRect.Bottom += document.BottomMargin; 

     content.SetColorStroke(BaseColor.BLACK); 
     content.Rectangle(pageBorderRect.Left, pageBorderRect.Bottom, pageBorderRect.Width, pageBorderRect.Height); 
     content.Stroke(); 
    } 
    private static void addCell(PdfPTable table, string text, int rowspan) 
    { 
     BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); 
     iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK); 

     PdfPCell cell = new PdfPCell(new Phrase(text, times)); 
     cell.Rowspan = rowspan; 
     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; 
     cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; 
     table.AddCell(cell); 
    } 
+0

あなたのコードをテストしました。できます。私は6つの表のセルと "ヘッダー3列にまたがる" pdfを取得します。だから問題はあなたのスニペットではありません。どのフォルダに – VDWWD

+0

あなたはPDFを取っている?コードが正しく動作するので私は尋ねていますが、どこでもpdfを見つけることはできません – Equilibrium

+0

ブラウザで開くか保存するかどうかを確認するメッセージが表示されます。 – VDWWD

答えて

0

をあなたは、このようなPDFファイルをエクスポート

function exportPDF() { 
     location.href = "/exportPDF.ashx"; 
    } 

Javascriptがリダイレクトを行い、レスポンスはファイルなので、ブラウザは何をすべきかを(開く/保存する)プロンプトを表示し、同じものにとどまりますページ。

あなたのコードはPDFを生成しますので、そのまま使用することができます。

関連する問題