CSV files are one of the most common formats for storing structured data in business and development environments. However, plain-text CSV tables often lack visual appeal and readability when shared or displayed. PDF files, on the other hand, support well-formatted table layouts, are read-only by default, and work across devices, making them ideal for reports, data archiving, and document sharing.
This guide provides a step-by-step solution for converting CSV files to PDF in Java , covering library setup, single-file conversion, style customization, and batch processing.
Why Convert CSV to PDF in Java
Converting CSV files to PDF has several advantages:
- Better readability : PDFs display data in structured tables rather than plain text, making content easier to read.
- Data security : PDFs are read-only by default, preventing accidental modification when sharing.
- Cross-platform compatibility : PDF files can open on almost any device or OS without requiring spreadsheet software.
- Archiving and reporting : PDFs are suitable for long-term storage and professional report distribution.
Install a CSV-to-PDF Library for Java
To convert CSV to PDF, you need a library that can handle both CSV reading and PDF output. This tutorial uses Spire.XLS for Java . If you are using Maven, add the following dependencies to your pom.xml:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>15.12.15</version>
</dependency>
</dependencies>
Once installed, you can use the library to load CSV files and export them as PDFs.
Convert a Single CSV File to PDF
The basic workflow is to load the CSV into a Workbook object and then save it as a PDF:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ConvertCsvToPdf {
public static void main(String[] args) {
Workbook wb = new Workbook();
wb.loadFromFile("Sample.csv", ",");
wb.getConverterSetting().setSheetFitToPage(true);
Worksheet sheet = wb.getWorksheets().get(0);
for (int i = 1; i < sheet.getColumns().length; i++) {
sheet.autoFitColumn(i);
}
sheet.saveToPdf("toPDF.pdf");
System.out.println("PDF generated: toPDF.pdf");
}
}
This method is ideal for converting individual CSV files while maintaining layout and readability.
Apply Styles for Better PDF Output
To enhance PDF presentation, you can customize font, borders, background colors, and pagination:
import com.spire.xls.*;
Workbook wb = new Workbook();
wb.loadFromFile("Sample.csv", ",");
Worksheet sheet = wb.getWorksheets().get(0);
CellRange range = sheet.getRange("A1:C10");
// Set bold font and size
range.getStyle().getFont().setBold(true);
range.getStyle().getFont().setSize(12);
// Add borders and background color
range.getStyle().getBorders().setAllBorders(BorderLineStyle.Thin);
range.getStyle().setColor(java.awt.Color.LIGHT_GRAY);
sheet.saveToPdf("styled_output.pdf");
Styling improves readability and ensures professional-looking PDFs.
Batch Convert Multiple CSV Files to PDF
To handle multiple CSV files, iterate through a folder and generate PDFs automatically:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import java.io.File;
public class BatchCsvToPdf {
public static void main(String[] args) {
String inputDir = "csv_files";
String outputDir = "pdf_files";
new File(outputDir).mkdirs();
File folder = new File(inputDir);
for (File csvFile : folder.listFiles((d, name) -> name.endsWith(".csv"))) {
Workbook wb = new Workbook();
wb.loadFromFile(csvFile.getAbsolutePath(), ",");
wb.getConverterSetting().setSheetFitToPage(true);
Worksheet sheet = wb.getWorksheets().get(0);
for (int i = 1; i < sheet.getColumns().length; i++) {
sheet.autoFitColumn(i);
}
String pdfFile = outputDir + "/" + csvFile.getName().replace(".csv", ".pdf");
sheet.saveToPdf(pdfFile);
}
System.out.println("Batch CSV-to-PDF conversion completed!");
}
}
Batch conversion is ideal for generating multiple reports efficiently.
Tips for CSV to PDF Conversion
- Keep CSV data clean: Avoid empty rows, merged cells, or special characters.
- Adjust column widths and fonts to enhance readability.
- Check pagination for large tables to prevent content overflow.
- Preview PDFs before final distribution to ensure proper layout.
Conclusion
Converting CSV files to PDF in Java allows structured data to be presented clearly in professional documents. By following these steps, you can perform single or batch conversions, apply custom styling, and produce readable, shareable PDFs suitable for reports, archiving, or data sharing.
Top comments (0)