Skip to content
Mohammad Badar Hashimi edited this page Jan 16, 2020 · 2 revisions

1. PDF Generation with InputStream


	@Autowired
	private PDFGenerator pdfGenerator;

	String inputFile = classLoader.getResource("csshtml.html").getFile();
	InputStream is = new FileInputStream(inputFile);
	ByteArrayOutputStream bos = (ByteArrayOutputStream) pdfGenerator.generate(is);
	String outputPath = System.getProperty("user.dir");
	String fileSepetator = System.getProperty("file.separator");
	File OutPutPdfFile = new File("csshtml.pdf");
	FileOutputStream op = new FileOutputStream(OutPutPdfFile);
	op.write(bos.toByteArray());
	op.flush();



2. PDF Generation with Template as String & OutStream


	@Autowired
	private PDFGenerator pdfGenerator;

	ClassLoader classLoader = getClass().getClassLoader();
	String inputFileName = classLoader.getResource("test.html").getFile();
	BufferedReader br = new BufferedReader(new FileReader(inputFileName));
	String line;
	StringBuilder sb = new StringBuilder();
	while ((line = br.readLine()) != null) {
		sb.append(line.trim());
	}

	ByteArrayOutputStream bos = (ByteArrayOutputStream) pdfGenerator.generate(sb.toString());
	String outputPath = System.getProperty("user.dir");
	String fileSepetator = System.getProperty("file.separator");
	File OutPutPdfFile = new File("test.pdf");
	FileOutputStream op = new FileOutputStream(OutPutPdfFile);
	op.write(bos.toByteArray());
	op.flush();


3. PDF Generation with File


	@Autowired
	private PDFGenerator pdfGenerator;

	String outputPath = System.getProperty("user.dir");
	String outputFileExtension = ".pdf";
	String fileSepetator = System.getProperty("file.separator");
	ClassLoader classLoader = getClass().getClassLoader();
	String inputFile = classLoader.getResource("textcontant.txt").getFile();
	String generatedPdfFileName = "textcontant";
	pdfGenerator.generate(inputFile, "", generatedPdfFileName);


4. PDF Generation with InputStream Passing Resource Location


	@Autowired
	private PDFGenerator pdfGenerator;

	ClassLoader classLoader = getClass().getClassLoader();
	String inputFile = classLoader.getResource("responsive.html").getFile();
	File file = new File(inputFile);
	if (file.getParentFile().isDirectory()) {
		file = file.getParentFile();
	}
	String resourceLoc = file.getAbsolutePath();
	InputStream is = new FileInputStream(inputFile);
	ByteArrayOutputStream bos = (ByteArrayOutputStream) pdfGenerator.generate(is, resourceLoc);
	String outputPath = System.getProperty("user.dir");
	String fileSepetator = System.getProperty("file.separator");
	File OutPutPdfFile = new File("responsive.pdf");
	FileOutputStream op = new FileOutputStream(OutPutPdfFile);
	op.write(bos.toByteArray());
	op.flush();


5. PDF Generation with Password Protected


	@Autowired
	private PDFGenerator pdfGenerator;

	StringBuilder htmlString = new StringBuilder();
        htmlString.append("<html><body> This is HMTL to PDF conversion Example</body></html>");
        InputStream is = new ByteArrayInputStream(htmlString.toString().getBytes());
	ByteArrayOutputStream outputStream = (ByteArrayOutputStream) 
        pdfGenerator.generate(is,"userpassword".getBytes());
	File file = new File("protected.pdf");
	FileUtils.writeByteArrayToFile(file, outputStream.toByteArray());


6. PDF Generation without Password

	@Autowired
	private PDFGenerator pdfGenerator;

	ByteArrayOutputStream outputStream = (ByteArrayOutputStream) 
        pdfGenerator.generate(null,"userpassword".getBytes());
	File file = new File("protected.pdf");
	FileUtils.writeByteArrayToFile(file, outputStream.toByteArray());

6. Merge PDF files


	@Autowired
	private PDFGenerator pdfGenerator;

	List<URL> pdfFiles = new ArrayList<URL>(Arrays.asList(PDFGeneratorTest.class.getResource("/sample.pdf"),
				PDFGeneratorTest.class.getResource("/pdf-sample.pdf")));
	byte[] byteArray = pdfGenerator.mergePDF(pdfFiles);

	String outputPath = System.getProperty("user.dir");
	String fileSeperator = System.getProperty("file.separator");
	File OutPutPdfFile = new File("new_merged.pdf");
	FileOutputStream op = new FileOutputStream(OutPutPdfFile);
	op.write(byteArray);
	op.flush();