Spring boot- Generate QR Code and Integrate it in PDF File with ZXing, and iText

Hello everyone, today we will learn how to generate a QR Code and integrate it in a PDF file with ZXing, and iText in the spring boot application.


Technology Used

  • JDK 17
  • Spring Boot 2.5.5
  • ZXing library 3.5.0
  • iText PDF 5.5.13.3
  • Maven

Final Project Directory



Maven[Pom.xml]

ZXing Core: Core barcode encoding/decoding library.
ZXing Java SE extensions: Java SE-specific extensions to the core ZXing library.
iText is a library for creating and manipulating PDF files in Java and .NET.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.knf.dev</groupId>
<artifactId>springboot-zxing-itext-qrcode-pdf</artifactId>
<version>0.0.1</version>

<name>springboot-zxing-itext-qrcode-pdf</name>
<description>Spring Boot Demo Application</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath />
</parent>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>


<
dependency>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-starter-test</artifactId>
<
scope>test</scope>
</
dependency>
</
dependencies>
<
build>
<
plugins>
<
plugin>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-maven-plugin</artifactId>
</
plugin>
</
plugins>
</
build>
</
project>



Create PDF Generator

package com.knf.dev.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Component;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

@Component
public class PDFGenerator {

public InputStreamResource InputStreamResource
(byte[] pngData)
throws DocumentException,
MalformedURLException, IOException {

ByteArrayOutputStream out = new ByteArrayOutputStream();

Document document = new Document();
PdfWriter.getInstance(document, out);
document.open();

Font font = FontFactory
.getFont(FontFactory.COURIER,
14, BaseColor.BLACK);
Paragraph para = new Paragraph("Scan QR Code", font);
para.setAlignment(Element.ALIGN_CENTER);
document.add(para);
document.add(Chunk.NEWLINE);

Image image = Image.getInstance(pngData);
image.scaleAbsolute(170f, 170f);
image.setAlignment(Element.ALIGN_CENTER);

document.add(image);
document.close();
ByteArrayInputStream bis = new ByteArrayInputStream
(out.toByteArray());
return new InputStreamResource(bis);
}
}



Create QRCode Generator

package com.knf.dev.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.springframework.stereotype.Component;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

@Component
public class QRCodeGenerator {

public byte[] getQRCode(String text, int width, int height)
throws WriterException, IOException {

QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter
.encode(text, BarcodeFormat.QR_CODE,
width, height);
ByteArrayOutputStream pngOutputStream =
new ByteArrayOutputStream();
MatrixToImageConfig con =
new MatrixToImageConfig(0xFF000002, 0xFF04B4AE);
MatrixToImageWriter.writeToStream
(bitMatrix, "PNG", pngOutputStream, con);
byte[] pngData = pngOutputStream.toByteArray();

return pngData;
}
}



Create TestController

package com.knf.dev.controller;

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.zxing.WriterException;
import com.itextpdf.text.DocumentException;
import com.knf.dev.util.PDFGenerator;
import com.knf.dev.util.QRCodeGenerator;

@RestController
public class TestController {

@Autowired
QRCodeGenerator qrCodeGenerator;

@Autowired
PDFGenerator pdfGenerator;

@GetMapping(value = "/export",
produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<InputStreamResource> employeeReport
(@RequestParam(defaultValue = "Hello") String text)
throws IOException, DocumentException, WriterException {

byte[] pngData = qrCodeGenerator
.getQRCode(text, 0, 0);
InputStreamResource inputStreamResource = pdfGenerator
.InputStreamResource(pngData);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; "
+ "filename=my-file.pdf");

return ResponseEntity.ok().headers(headers)
.contentType(MediaType.APPLICATION_PDF)
.body(inputStreamResource);
}
}



Spring Boot Main Driver

package com.knf.dev;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


Download the complete source code - click here                          

Local Setup and Run the application

Step1: Download or clone the source code from GitHub to the local machine - Click here

Step 2: mvn clean install

Step 3: Run the Spring Boot application - mvn spring-boot:run

From the browser call the endpoint http://localhost:8080/export?text=knowledgefactory



All the best!

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download Example

Node JS mini projects with source code - free download