Saturday, February 20, 2021

How to Create PDF in Java Using OpenPDF

In the post Creating PDF in Java Using iText we have already seen how to use iText library to generate a PDF in Java. Itext is one of the best way to generate PDF in Java, has many features but there is one problem; it is AGPL licensed which means you must distribute all source code, including your own product and web-based applications. Many times the idea to use iText is shot down by the clients because of this reason. So, in this post we’ll see one alternative of using iText for generating PDF in Java. That option is OpenPDF for generating PDF.

OpenPDF for creating PDF in Java

OpenPDF is a free Java library for creating and editing PDF files with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. In fact you will find the code for generating PDF using OpenPDF quite similar to iText API till version 5. OpenPDF is actively maintained and a very good option for creating PDF in Java.

Maven dependecy

For using OpenPDF library you must add the following dependencies to your pom.xml file.

For Java 8 onward-

<dependency>
  <groupId>com.github.librepdf</groupId>
  <artifactId>openpdf</artifactId>
  <version>1.2.4</version>
</dependency>
Java 7 compatible branch-
<dependency>
  <groupId>com.github.librepdf</groupId>
  <artifactId>openpdf</artifactId>
  <version>1.2.3.java7</version>
</dependency>

Friday, February 19, 2021

Convert HTML to PDF in Java + Openhtmltopdf and PDFBox

In this tutorial we’ll see how to convert HTML to PDF in Java using Openhtmltopdf and PDFBox.

Check another option to convert HTMP to PDF in this post- HTML to PDF in Java + Flying Saucer and OpenPDF

How does it work

Let’s first understand what do the libraries mentioned here do-

  1. Open HTML to PDF is a pure-Java library for rendering arbitrary well-formed XML/XHTML (and even HTML5) using CSS 2.1 for layout and formatting, outputting to PDF or images.
  2. jsoup library is used for parsing HTML using the best of HTML5 DOM methods and CSS selectors. That gives you a well formed HTML (XHTML) that can be passed to the Openhtmltopdf.
  3. Openhtmltopdf uses the open-source PDFBOX as PDF library which generates PDF document from the rendered representation of the XHTML returned by Openhtmltopdf.

Thursday, February 18, 2021

Creating PDF in Java Using Apache PDFBox

In the post Creating PDF in Java Using iText we have already seen how to use iText library to generate a PDF in Java, we have already seen one alternative of iText which is OpenPDF for generating PDF. In this tutorial we’ll learn about another option for generating PDF in Java using Apache PDFBox.

PDFBox for creating PDF in Java

The Apache PDFBox library (https://pdfbox.apache.org/) is an open source tool written in Java for working with PDF documents. Using PDFBox you can create new PDF documents, manipulate existing documents and extract content from PDF documents. Apache PDFBox also includes several command-line utilities. Apache PDFBox is published under the Apache License v2.0.

Wednesday, February 17, 2021

Java Program to Find First Non-Repeated Character in a Given String

A very popular interview question for String is to write a Java program to find first non-repeated character in a given String. For example if given string is “always” then first non-repeated character is ‘l’ as character ‘a’ is repeated. Same way if String is “net” then first non-repeated character is ‘n’.

There are many ways to solve this problem, in this post I am giving 4 solutions to find first non-repeated character in a given String using Java.

  1. If you are asked not to use any inbuilt API or data structure.
  2. Solution using LinkedHashMap.
  3. Solution using indexof() method of the String class.
  4. Java 8 onward you can also use Stream API

Wednesday, February 10, 2021

HTML to PDF in Java + Flying Saucer and OpenPDF

In your application you may come across a scenario to convert the HTML to PDF on the fly. In this tutorial we’ll see how to convert HTML to PDF in Java using Flying Saucer and OpenPDF.

Check another option to convert HTMP to PDF in this post- Convert HTML to PDF in Java + Openhtmltopdf and PDFBox

How does it work

Let’s first understand which library is used for what purpose-

  1. Flying Saucer is an XML/CSS renderer, which means it takes XML files as input, applies formatting and styling using CSS, and generates a rendered representation of that XML as output. As an input you can pass an XHTML file which is an XML document format that standardizes HTML.
  2. jsoup library is used for parsing HTML using the best of HTML5 DOM methods and CSS selectors. That gives you a well formed HTML that can be passed to the Flying Saucer.
  3. Flying Saucer renders the input XHTML that still needs to be converted to PDF for that OpenPDF is used. OpenPDF is a free Java library for creating and editing PDF files with a LGPL and MPL open source license. OpenPDF is based on a fork of iText.

Tuesday, February 9, 2021

Invoke Method at Runtime Using Java Reflection API

In this post we’ll see how to invoke a method at runtime using Java reflection API. You can even call a private method using reflection.

How to get the method instance

In order to invoke a method first thing you need to do is to get an instance of the Method. Using the class object (instance of the class in which method is defined) you can invoke one of the following methods to get an instance of a single method or get all the methods of the class in an array.

  • getMethod(String name, Class<?>... parameterTypes)- Using getMethod() you can get a Method object for the specified public member method of the class (instance or static). As method arguments pass the method name and the type of the method parameters.
  • getMethods()- This method returns an array containing Method objects for all the public methods of the class even the inherited ones.
  • getDeclaredMethod(String name, Class<?>... parameterTypes)- Using getDeclaredMethod() you can get a Method object for the specified method of the class (having any access modifier public, private, protected, default).
  • getDeclaredMethods()- This method returns an array containing Method objects for all the declared methods of the class including public, protected, default (package) access, and private methods, but excluding inherited methods.

Monday, February 8, 2021

Angular + Spring Boot JWT Authentication Example

In this tutorial we’ll create a full login example with authentication using Angular, Spring Boot, Spring Security, JWT authentication.

In the back end Spring Boot is used to create REST APIs, Spring security is used for authentication and authorization, token based authentication is done using JWT, Spring Data JPA is used for DB operations.

In the front end Angular is used to create forms, provide routing and Bootstrap is used for styling.

Back end Spring Boot service

In the backend we have a Spring Boot application that uses Spring Security and JWT token based authentication to bring authentication and authorization to the exposed REST APIs. DB used is MySQL.

To get more details and code for the Spring Boot + Spring Security JWT Authentication Example refer this post- https://www.netjstech.com/2021/02/spring-boot-spring-security-jwt-authentication.html

Only change that is required in the Spring boot application is to add @CrossOrigin annotation in the Controller class to enable cross-origin resource sharing (CORS). If you won't use this annotation cross browser HTTP request won't be allowed and the Spring Boot application won't accept any request coming from http://localhost:4200