Skip to content

Instantly share code, notes, and snippets.

@raylax
Created July 28, 2021 07:28
Show Gist options
  • Save raylax/c18dab12508f986faa51ad230e5dfb85 to your computer and use it in GitHub Desktop.
Save raylax/c18dab12508f986faa51ad230e5dfb85 to your computer and use it in GitHub Desktop.
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
public class ZipMultiplePdf {
private static final String PATH = "D:\\IDEA\\test\\src\\main\\resources\\";
private static final String[] FILES = { "test1.html", "test2.html", "test3.html" };
public static void main(String[] args) throws Exception {
ZipFile zipFile = new ZipFile("data.zip");
for (String file : FILES) {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(bao);
PdfDocument pdf = new PdfDocument(writer);
ConverterProperties props = new ConverterProperties();
Document document = HtmlConverter.convertToDocument(new FileInputStream(PATH + file), pdf, props);
document.close();
ZipParameters parameters = new ZipParameters();
parameters.setFileNameInZip(file.substring(0, file.lastIndexOf(".")) + ".pdf");
zipFile.addStream(new ByteArrayInputStream(bao.toByteArray()), parameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment