Advertisement
hisyam99

PDFReport

Jul 13th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | Source Code | 0 0
  1. package org.pbo.kasir;
  2.  
  3. import com.lowagie.text.Font;
  4. import com.lowagie.text.*;
  5. import com.lowagie.text.pdf.PdfPCell;
  6. import com.lowagie.text.pdf.PdfPTable;
  7. import com.lowagie.text.pdf.PdfWriter;
  8.  
  9. import java.awt.*;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.sql.*;
  14.  
  15. public class PDFReport {
  16.     public static void generatePDFReport() throws ClassNotFoundException, SQLException, DocumentException, FileNotFoundException {
  17.         int rowno = 0;
  18.         Class.forName("com.mysql.cj.jdbc.Driver");
  19.         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db_cafe", "root", "");
  20.         Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  21.         ResultSet result = st.executeQuery("SELECT * FROM transaksi");
  22.         ResultSetMetaData meta = result.getMetaData();
  23.         int colno = meta.getColumnCount();
  24.         while (result.next()) {
  25.             rowno = rowno + 1;
  26.         }
  27.         result.first();
  28.  
  29.         String fileName = "report.pdf";
  30.         File file = new File(fileName);
  31.         int count = 1;
  32.         while (file.exists()) {
  33.             fileName = "report" + count + ".pdf";
  34.             file = new File(fileName);
  35.             count++;
  36.         }
  37.  
  38.         Document d = new Document();
  39.         PdfWriter.getInstance(d, new FileOutputStream(fileName));
  40.         d.open();
  41.  
  42.         // Judul Laporan
  43.         Font fontJudul = new Font(Font.HELVETICA, 18, Font.BOLD, new Color(0, 0, 255));
  44.         Paragraph judul = new Paragraph("Struk Belanja", fontJudul);
  45.         judul.setAlignment(Element.ALIGN_CENTER);
  46.         d.add(judul);
  47.         d.add(new Paragraph("\n"));
  48.  
  49.         // Tabel
  50.         PdfPTable pt = new PdfPTable(colno);
  51.         pt.setWidthPercentage(100);
  52.         pt.setSpacingBefore(10f);
  53.         pt.setSpacingAfter(10f);
  54.  
  55.         // Header Tabel
  56.         Font fontHeader = new Font(Font.HELVETICA, 12, Font.BOLD, new Color(255, 255, 255));
  57.         PdfPCell headerCell;
  58.         for (int i = 1; i <= colno; i++) {
  59.             headerCell = new PdfPCell(new Phrase(meta.getColumnName(i), fontHeader));
  60.             headerCell.setBackgroundColor(new Color(0, 0, 255));
  61.             headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  62.             headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  63.             pt.addCell(headerCell);
  64.         }
  65.  
  66.         // Isi Tabel
  67.         Font fontIsi = new Font(Font.HELVETICA, 10);
  68.         PdfPCell isiCell;
  69.         do {
  70.             for (int j = 1; j <= colno; j++) {
  71.                 isiCell = new PdfPCell(new Phrase(result.getString(j), fontIsi));
  72.                 isiCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  73.                 isiCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  74.                 pt.addCell(isiCell);
  75.             }
  76.         } while (result.next());
  77.  
  78.         // Tambahkan Tabel ke Dokumen
  79.         d.add(pt);
  80.         d.close();
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement