I have faced this problem lots of times, the same in JavaScript, C#, or in Java, normally the requirements are the same. To allow create reports some of them being text based only, but other seems to be more complex, having formatted text, tables, images and even charts. It is well know that there are many reporting tools, for example, DevExpress XtraReports, Crystal Reports, Jasper Reports, MS Reporting Services, and even more MS Word Interop for creating Docx / PDF custom reports, MS Excel Interop, but none of that technologies is as simple and powerful as iText (for Java) or iTextSharp (for .Net). That library allow you to create powerful PDF documents, reports, books, etc. Take a look at iText in Action book to have a good idea of all the things you can do with iText. Downloading the PDF to the client side is a matter of flushing the PDF stream using the appropiate MIME type: "application/pdf".
public Image CreateNewLogo()
{
string imageURL = HttpContext.Current.Server.MapPath(PDFResources.ImagesPath) + "/Logo.png";
Image img = Image.GetInstance(imageURL);
img.ScaleToFit(270f, 90f);
img.SpacingBefore = 5f;
img.SpacingAfter = 5f;
img.Alignment = Element.ALIGN_LEFT;
return img;
}
public void CreateHeader()
{
string div = Resources.OFR_Resources.h1_div + " " + data.DescDivision;
string zona = Resources.OFR_Resources.h1_zona + " " + data.DescZona;
Image logo = CreateNewLogo();
PdfPTable table = new PdfPTable(2); table.DefaultCell.Border = Rectangle.NO_BORDER;
PdfPCell cellDZ = new PdfPCell(); cellDZ.Border = Rectangle.NO_BORDER;
Paragraph pd = NewParagraphHeader(div); pd.Alignment = Element.ALIGN_RIGHT;
Paragraph pz = NewParagraphHeader(zona); pz.Alignment = Element.ALIGN_RIGHT;
cellDZ.AddElement(pd); cellDZ.AddElement(pz);
table.AddCell(logo); table.AddCell(cellDZ);
document.Add(table);
}
public void CreateContent()
{
string s1_p1 = Resources.OFR_Resources.s1_p1.Replace("[FECHA_SOLICITUD]", string.Format("{0: dd MM yyyy}", data.Solicitud.fechaAlta));
s1_p1 = s1_p1.Replace("[RAZON_SOCIAL]", data.Solicitud.dsNombreDenRS);
s1_p1 = s1_p1.Replace("[DIRECCION_SOLICITANTE]", data.DireccionSolicitante);
PdfPTable table = new PdfPTable(2); table.DefaultCell.Border = Rectangle.NO_BORDER;
Paragraph p_fecha = NewParagraphN(Resources.OFR_Resources.s1_fecha.Replace("[FECHA_ACTUAL]", DateTime.Now.ToLongDateString()));
Paragraph p_oficio = NewParagraphN(Resources.OFR_Resources.s1_oficio);
Paragraph p_asunto = NewParagraphN(Resources.OFR_Resources.s1_asunto);
PdfPCell cellRigth = new PdfPCell();
cellRigth.AddElement(p_fecha);
cellRigth.AddElement(p_asunto);
cellRigth.Border = Rectangle.NO_BORDER;
table.AddCell("");
table.AddCell(cellRigth);
document.Add(table);
}
See: https://itextpdf.com/