使用itextsharp创建PDF不适用于客户端的服务器

时间:2013-08-10 10:14:07

标签: asp.net itextsharp

我试图根据某些条件动态构建一个pdf,它在我们的机器和我们客户的机器之间运行良好。但是对于另一个客户,当我们部署系统时,pdf没有正确生成。我有以下代码来生成此

public void pdf_ModerateData(string name, string dob, string gender, string age)
{

    Phrase pat_name = new Phrase();
    Phrase phr_paitient_name = new Phrase();
    Phrase phr_paitient_dob = new Phrase();


    Paragraph pat_det = new Paragraph();
    Paragraph paitient_name_dob = new Paragraph();
    Paragraph mul_column = new Paragraph();

    iTextSharp.text.Font fntNormalText = FontFactory.GetFont(FontFactory.TIMES, 12, iTextSharp.text.Font.NORMAL);
    iTextSharp.text.Font fntBoldText = FontFactory.GetFont(FontFactory.TIMES, 12, iTextSharp.text.Font.BOLD);
    iTextSharp.text.Font fntsmallText = FontFactory.GetFont(FontFactory.TIMES, 8, iTextSharp.text.Font.NORMAL);



    phr_paitient_name = new Phrase(System.Environment.NewLine + System.Environment.NewLine + name + "                                                                                      " + dob, fntNormalText);

    pat_name = new Phrase(System.Environment.NewLine + "               " + System.Environment.NewLine + "                                                                                                                                                            ", fntsmallText);

    pat_det.Add(phr_paitient_name);
    pat_det.Add(pat_name);
    Phrase emt = new Phrase();
    Paragraph emty = new Paragraph();
    emt = new Phrase(System.Environment.NewLine + "    ", fntNormalText);
    emty.Add(emt);


    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Document pdfDoc = new Document();
    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

    pdfDoc.Open();


    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:/trial/dms/img/moderatetool.jpg");
    logo.ScaleAbsolute(475, 600);
    PdfPTable table = new PdfPTable(1);

    table.TotalWidth = 500f;

    table.LockedWidth = true;

    iTextSharp.text.Image barcode = iTextSharp.text.Image.GetInstance("C:/trial/dms/barcode/brcode.gif");
    barcode.ScaleAbsolute(100, 50);


    PdfPTable nested = new PdfPTable(2);
    mul_column.Add(pat_det);

    PdfPCell mul = new PdfPCell(mul_column);
    mul.HorizontalAlignment = Element.ALIGN_LEFT;
    mul.BorderWidth = 0;
    nested.AddCell(mul);
    //PdfPCell imag = new PdfPCell(barcode);
    //imag.HorizontalAlignment = Element.ALIGN_RIGHT;
    //imag.BorderWidth = 0;
    //nested.AddCell(imag);
    PdfPCell imag = new PdfPCell(barcode);
    imag.HorizontalAlignment = Element.ALIGN_RIGHT;
    imag.BorderWidth = 0;
    nested.AddCell(imag);
    PdfPTable nested1 = new PdfPTable(2);
    //mul_column.Add(pat_det);

    PdfPCell mul1 = new PdfPCell(emty);
    mul1.HorizontalAlignment = Element.ALIGN_LEFT;
    mul1.BorderWidth = 0;
    nested1.AddCell(mul1);
    PdfPCell imag1 = new PdfPCell(barcode);
    imag1.HorizontalAlignment = Element.ALIGN_RIGHT;
    imag1.BorderWidth = 0;
    nested1.AddCell(imag1);



    PdfPCell nesthousing = new PdfPCell(nested1);
    nesthousing.Padding = 0f;
    nesthousing.BorderWidth = 0;
    table.AddCell(nesthousing);

    PdfPCell image_header = new PdfPCell(logo);
    image_header.HorizontalAlignment = Element.ALIGN_CENTER;
    image_header.BorderWidth = 0;

    table.AddCell(image_header);

    Paragraph para = new Paragraph();

    //string pat = get_patientDET();

    //para = new Paragraph(pat);


    PdfPCell header = new PdfPCell(para);
    header.HorizontalAlignment = Element.ALIGN_CENTER;
    header.Colspan = 4;
    header.BorderWidth = 0;
    //table.AddCell(header);
    PdfPCell nesthousing1 = new PdfPCell(nested);
    nesthousing.Padding = 0f;
    nesthousing.BorderWidth = 0;
    nesthousing1.BorderColor = BaseColor.WHITE;
    table.AddCell(nesthousing1);


    pdfDoc.Add(table);


    pdfDoc.Close();

    HttpContext.Current.Response.Write(pdfDoc);
    HttpContext.Current.Response.End();
}

任何人都可以指导我解决这个问题。

服务器:Windows 2008 R2数据中心64位servicepack1

当我们下载pdf时,它下载为 “Gridviewreoprt.pdf,附件”并无法正常工作。

1 个答案:

答案 0 :(得分:2)

问题可能由以下原因导致99%的可能性:

iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:/trial/dms/img/moderatetool.jpg");
iTextSharp.text.Image barcode = iTextSharp.text.Image.GetInstance("C:/trial/dms/barcode/brcode.gif");

您的客户会疯狂地让您访问其服务器的C:驱动器。开发人员应避免在其代码中向C:驱动器上的文件添加硬编码路径。您应该在Web应用程序中封装所有资源(图像,条形码,PDF模板......),以便可以在每台服务器上部署您的应用程序,而不需要存储和访问存储在服务器上其他位置的文件。