Convert HTML to PDF

Why do we need to convert the HTML into PDF?

Let you can consider you have been working in one e-commerce project. That definitely has the option or page to customer can check the their orders. There you have to display the order history along with the product image, quantity price per item discount etc. As a developer, you know how difficult to bring that page as per your client requires. You have to add many HTML tags and CSS. If your client gives requirement that the customer can download and see the their orders in PDF format and that pdf should be same as your HTML design. In this case you have to spend more time to designing the PDF and more hard to bring same design. So here most easiest way is converting the HTML code into PDF.

In this blog, I have given you the sample code for converting the HTML into PDF. For my example, I have been using the ITEXT PDF plugin. My back end code is completely in Java Spring Boot. and my front end is in Angular

If you are beginner before you continue further you should know how to create Angular application and how to create spring boot applications. If you do not know about it, please visit below links


<!-- iText Core -->

		<!-- iText pdfHTML add-on -->

	private ResponseEntity<Resource> htmlToPdf() throws IOException{
		ByteArrayInputStream inputStream = null;
		StringBuffer buffer = new StringBuffer();
		buffer.append("<h1 style='color:red;'>Students List</h1>");
		for(int i = 1; i<=10; i++) {
		String html = buffer.toString();
		ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
		HtmlConverter.convertToPdf(html, pdfStream);
		inputStream = new ByteArrayInputStream(pdfStream.toByteArray());
		Resource resource = new InputStreamResource(inputStream);
	 	String fileName ="ddMMyyyyhhmmss"))+".pdf";
	 	return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")

angular – student.component.ts

      this.studentService.downloadPdf().subscribe(data => {
        let blob = new Blob([data], {
          type: 'application/pdf' // must match the Accept type
        let url = window.URL.createObjectURL(blob);


angular – student.service.ts

downloadPdf(): Observable<any> {
    let headerOptions = new HttpHeaders({
      'Content-Type': 'application/json',
      'Accept': 'application/pdf'
    let requestOptions = { headers: headerOptions, responseType: 'blob' as 'blob'};
    return this.http.get('/api/student/htmlToPdf', requestOptions);


In this blog, I have posted the sample code for converting the your HTML code into the PDF, by using the Java Spring Boot as back end and Angular as a front end. Hope you have got some idea about how to convert the HTML to PDF. After you read this blog, I believe you got the expected solution for your problem. And now you can implement same in your project and get smiles from your client.