Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate pdf file after retrieving the information [closed]

I need to create a pdf file from data obtained from a database. I have to retrieve the data from database and generate these data as a pdf file.

I would like to have pointers (helpful information).

Thank you.

like image 430
As k Avatar asked Dec 07 '22 22:12

As k


1 Answers

Here's a list of Open Source PDF Libraries that you can use:

  • SharpPDF
  • iTextSharp
  • Report.NET
  • PDFjet

Example of PDFjet

PDF pdf = new PDF();
Font f1 = new Font(pdf, "Helvetica");
Image image1 = new Image(pdf, "images/eu-map.png");
Image image2 = new Image(pdf, "images/fruit.jpg");
Image image3 = new Image(pdf, "images/mt-map.gif");
// Please note:
// All font and image objects must be created
// before the first page object.


Page page = new Page(pdf, A4.PORTRAIT);

text.SetText(
        "The map on the right is an embedded GIF image");
text.SetPosition(90.0, 800);
text.DrawOn(page);

image3.SetPosition(390, 630);
image3.ScaleBy(0.5);
image3.DrawOn(page);

pdf.wrap();
pdf.save("Example_03.pdf");

Example of SharpPDF

pdfDocument myDoc = new pdfDocument("TUTORIAL","ME");
pdfPage myPage = myDoc.addPage();
myPage.addText("Hello World!",200,450,predefinedFont.csHelvetica,20);
myDoc.createPDF(@"c:\test.pdf");
myPage = null;
myDoc = null; 

Example of Report.NET

Report report = new Report(new PdfFormatter());
FontDef fd = new FontDef(report, "Helvetica");
FontProp fp = new FontPropMM(fd, 25);
Page page = new Page(report);
page.AddCenteredMM(80, new RepString(fp, "Hello World!"));
RT.ViewPDF(report, "HelloWorld.pdf");

Example of iTextSharp

Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
document.open();  
document.add(new Paragraph("Hello Pdf"));
document.close(); 

Returning on the fly created PDF-files

You can return Binary Data using Response.Write see MSDN on "How To Write Binary Data"

Here is an example on how you use Response.WriteFile to give the user a PDF:

//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
like image 79
Filip Ekberg Avatar answered Apr 05 '23 23:04

Filip Ekberg