All Projects → farrukhmpk → Html Pdf Service

farrukhmpk / Html Pdf Service

Licence: other
LGPL V3. Java Spring Boot microservice with RESTful webconsole and service endpoints that convert HTML to PDF, optionally styling with CSS and templating with JSON using Flying Saucer, PDF Box and Jackson libraries. Available on Docker Hub.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Html Pdf Service

Problem Spring Web
A library for handling Problems in Spring Web MVC
Stars: ✭ 636 (+5200%)
Mutual labels:  microservice, json, spring-boot
Html Pdf Chrome
HTML to PDF converter via Chrome/Chromium
Stars: ✭ 629 (+5141.67%)
Mutual labels:  pdf, pdf-generation
Clj Pdf
PDF generation library for Clojure
Stars: ✭ 571 (+4658.33%)
Mutual labels:  pdf, pdf-generation
Microservices Event Sourcing
Microservices Event Sourcing 是一个微服务架构的在线购物网站,使用Spring Boot、Spring Cloud、Spring Reactor、OAuth2、CQRS 构建,实现了基于Event Sourcing的最终一致性,提供了构建端到端微服务的最佳实践
Stars: ✭ 657 (+5375%)
Mutual labels:  microservice, spring-boot
Getty
a netty like asynchronous network I/O library based on tcp/udp/websocket; a bidirectional RPC framework based on JSON/Protobuf; a microservice framework based on zookeeper/etcd
Stars: ✭ 532 (+4333.33%)
Mutual labels:  microservice, json
Combine pdf
A Pure ruby library to merge PDF files, number pages and maybe more...
Stars: ✭ 552 (+4500%)
Mutual labels:  pdf, pdf-generation
Brpc Java
Java implementation for Baidu RPC, multi-protocol & high performance RPC.
Stars: ✭ 647 (+5291.67%)
Mutual labels:  microservice, spring-boot
Spring Boot Study
SpringBoot框架源码实战(已更新到springboot2版本实现)~基本用法,Rest,Controller,事件监听,连接数据库MySQL,jpa,redis集成,mybatis集成(声明式与xml两种方式~对应的添删查改功能),日志处理,devtools配置,拦截器用法,资源配置读取,测试集成,Web层实现请求映射,security安全验证,rabbitMq集成,kafka集成,分布式id生成器等。项目实战:https://github.com/hemin1003/yfax-parent 已投入生产线上使用
Stars: ✭ 440 (+3566.67%)
Mutual labels:  restful-api, spring-boot
Satysfi
A statically-typed, functional typesetting system
Stars: ✭ 815 (+6691.67%)
Mutual labels:  pdf, pdf-generation
Itext7
iText 7 for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText 7 can be a boon to nearly every workflow.
Stars: ✭ 913 (+7508.33%)
Mutual labels:  pdf, pdf-generation
Spring Qrcode Example
Demonstrates some of the capabilities of the Spring Boot framework through a small, simple example.
Stars: ✭ 23 (+91.67%)
Mutual labels:  microservice, spring-boot
Dart pdf
Pdf creation module for dart/flutter
Stars: ✭ 500 (+4066.67%)
Mutual labels:  pdf, pdf-generation
Cerberus
A demonstration of a completely stateless and RESTful token-based authorization system using JSON Web Tokens (JWT) and Spring Security.
Stars: ✭ 482 (+3916.67%)
Mutual labels:  restful-api, spring-boot
Pikepdf
A Python library for reading and writing PDF, powered by qpdf
Stars: ✭ 566 (+4616.67%)
Mutual labels:  pdf, pdf-generation
Printable Mockups
Create printable UI mockups & wireframes templates
Stars: ✭ 479 (+3891.67%)
Mutual labels:  pdf, pdf-generation
Go Zero
go-zero is a web and rpc framework written in Go. It's born to ensure the stability of the busy sites with resilient design. Builtin goctl greatly improves the development productivity.
Stars: ✭ 13,156 (+109533.33%)
Mutual labels:  microservice, restful-api
Matecloud
🔥MateCloud是一款基于Spring Cloud Alibaba的微服务架构。目前已经整合Spring Cloud Alibaba、Spring Security Oauth2、Feign、Dubbo、JetCache、RocketMQ等服务套件,为您的开发保驾护航
Stars: ✭ 412 (+3333.33%)
Mutual labels:  microservice, spring-boot
One File Pdf
A minimalist Go PDF writer in 1982 lines. Draws text, images and shapes. Helps understand the PDF format. Used in production for reports.
Stars: ✭ 429 (+3475%)
Mutual labels:  pdf, pdf-generation
Itext7 Dotnet
iText 7 for .NET is the .NET version of the iText 7 library, formerly known as iTextSharp, which it replaces. iText 7 represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText 7 can be a boon to nearly every workflow.
Stars: ✭ 698 (+5716.67%)
Mutual labels:  pdf, pdf-generation
Hexapdf
Versatile PDF creation and manipulation for Ruby
Stars: ✭ 852 (+7000%)
Mutual labels:  pdf, pdf-generation

Author: Farrukh Mirza
Date: 13/07/2016
Location: Dublin, Republic of Ireland

Purpose

HTML-PDF-Service is a Java Spring Boot based microservice.
It exposes a RESTful interface to convert well-formed HTML Template to either PDF or well-formed HTML.
The service only expects HTML BODY contents to be sent over in the service calls under html parameter (HEAD section will be included by default).
The service allows the clients to optionally style the PDF output using CSS 2.1 style sheets.
The service also allows the clients to optionally use HTML as a Template with JSON data.

Even though there are many other similar projects available, the idea behind this microservice is to allow anyone to deploy it on existing Java based infrastructure within a public or private cloud environment.
The service allows the users to quickly build HTML page with custom styling, try it out from the web console and then simply insert variables in the HTML body which will be replaced by JSON object or JSON array provided as a separate argument to the service.
If a JSON array is provided with multiple JSON objects, then individual PDF outputs are merged together into a single PDF file and returned as such (try webconsole at {PROTOCOL}://{HOST:PORT}/html-pdf-service/).
In addition, this project also serves as a quick tutorial on building RESTful microservices and applications using Spring Boot in Java.

This service requires Servlet 3.0 servlet container like Tomcat 8 or Wildfly 9+.
This service uses:

  1. Flying Saucer Pdf library to convert html and css documents (strings) into PDF.
  2. Jackson to handle html templating with Json data.
  3. Apache PDF Box to merge multiple pdf files into one.
  4. JSON Path to traverse JSON data and parse values for templating.
  5. Bootstrap to style web page.
  6. JQuery to test and control page behaviour.
  7. A few other pieces of javascript code were borrowed from different sources and converted into JQuery plugin. The original authors are acknowledged and credited in the respective files.

Licenses

html-pdf-service (this service) is provided under LGPL version 3 or later License. Third party licenses are listed below.

  1. FlyingSaucer is provided under LGPL License version 3 which in turn uses iText 2.1.7 under Mozilla Public License Version 1.1.
  2. spring-boot is provided under APACHE License v2
  3. apache-commons-lang3 is provided under APACHE License v2
  4. pdfbox is provided under APACHE License v2
  5. jackson is provided under APACHE License v2
  6. jsonpath 2.2.0 is provided under APACHE License v2 (Applicable expressions can be found at https://github.com/json-path/JsonPath)

Docker

Docker image is available at Docker Hub

docker pull farrukhmpk/html-pdf-service

Build

Pre-requisites

1. JDK8
2. Maven
3. Git 

Compile, build and deploy

By Default the service is built for Apache Tomcat server mvn clean install.
deploy.bat and run.bat can also be used to deploy the war file to Tomcat and run Tomcat server, assuming {CATALINA_HOME} is setup correctly. Linux versions of these scripts can be created very easily.

  1. The service can be explicitly built for WildFly mvn clean install -Pwildfly.
  2. The service can be explicitly built for Apache Tomcat. Build using mvn clean install -Ptomcat.
  3. The service can be explicitly built for Embedded Tomcat Container. Build using mvn clean install -Pstandalone.

The main difference between the two profiles is the location of the log files.

Standalone deployment

This microservice can be very easily deployed as a standalone executable by enabling embedded Tomcat.

Simply build the project using standalone profile mvn clean install -Pstandalone and then run the resultant war java -jar target\html-pdf-service.war

Now you can access the test page by pointing your browser to http://localhost:8080/

Note: This, however, opens up the subject of port management in the production environment.

REST Endpoints

Endpoint Types

There are two types of endpoints.

1. Actual Endpoints.  
3. Management Endpoints (Spring Boot).  

Actual Endpoints

The service can be access from browser using {PROTOCOL}://{HOST:PORT}/html-pdf-service/. The main page serves as a testing area to try out different options.

The actual service endpoints use HTTP POST and GET protocols with slight variations.

The base endpoint for converting HTML Template into PDF is available at {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/convert/html/
The service can be invoked in two fashions:

  • The service can take the parameters in the request body by calling {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/convert/html/body. In this case, the request body can contain a json object with html, css, json fields, where css and json fields are optional, e.g., {"html": "< h1 >This is a title</ h1 > < p >This is body</ p >", "css": "h1{color:blue;}"} . This only supports POST requests.
  • The service can take the parameters as request parameters by calling {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/convert/html/params. In this case, the request parameters contain html, css, json fields, where css and json fields are optional, e.g., html="< h1 >This is a title</ h1 > < p >This is body</ p >"&css="h1{color:blue;}" .

If the service response is desired as a byte stream, the service endpoint can be appended by /byte, e.g.,

  • {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/convert/html/body/byte
  • {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/convert/html/params/byte

The base endpoint for converting a HTML Template into Fully Formed HTML is available at {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/template/html/
The service can be invoked in two fashions:

  • The service can take the parameters in the request body by calling {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/template/html/body. In this case, the request body can contain a json object with html, css, json fields, where css and json fields are optional, e.g., {"html": "< h1 >This is a title</ h1 > < p >This is body</ p >", "css": "h1{color:blue;}"} . This only supports POST requests.
  • The service can take the parameters as request parameters by calling {PROTOCOL}://{HOST:PORT}/html-pdf-service/service/template/html/params. In this case, the request parameters contain html, css, json fields, where css and json fields are optional, e.g., html="< h1 >This is a title</ h1 > < p >This is body</ p >"&css="h1{color:blue;}" .

This template service always responds with JSON Body which contains a list of formed HTML output.

Management Endpoints

These endpoints are provided by the spring-boot framework.
They are generally available at {PROTOCOL}://{HOST:PORT}/html-pdf-service/

1. <code>/health</code> shows general health of the application
2. <code>/info</code> shows custom service info (Not in use)

Endpoints

Base Application URL: {PROTOCOL}://{HOST:PORT}/html-pdf-service/ e.g., http://localhost:8080/html-pdf-service
Base Service URL: {PROTOCOL}://{HOST:PORT}/html-pdf-service/ e.g., http://localhost:8080/html-pdf-service/service/convert/html or http://localhost:8080/html-pdf-service/service/template/html

The service endpoints get appended to the base service endpoint url, e.g., http://localhost:8080/html-pdf-service/service/convert/html/params or http://localhost:8080/html-pdf-service/service/template/html/params.

  1. /params takes request parameters and responds with the file in the HTTP Servlet Response.
  2. /params/byte takes request parameters and will return a byte array of the generated PDF file.
  3. /body takes request body as json and responds with the file in the HTTP Servlet Response.
  4. /body/byte takes request body as json and will return a byte array of the generated PDF file.

USAGE & EXAMPLES

Printing Single Letter With Simple JSON

HTML

<h1>HELLO </h1> <p>{name.first} </p> <p>{name.second} </p>

JSON

{ "name": { "first": "Farrukh", "second": "Mirza" } }

Printing Multiple Letters With Simple JSON ARRAY

HTML

<h1>HELLO </h1> <p>{name.first} </p> <p>{name.second} </p>

JSON

[ { "name": { "first": "Farrukh", "second": "Mirza" } }, { "name": { "first": "Jack", "second": "Bauer" } } ]

Printing Single Letter With JSON Containing Repeated Data

In order to print, e.g., multiple rows in a table, use the <repeat> tag. The elements in the array can be referenced using [*] notation.

HTML

<h1>HELLO </h1> <table> <repeat> <tr> <td>{names[*].first} </td> <td>{names[*].second} </td> <tr> </repeat> </table>

JSON

{ "names": [ { "first": "Farrukh", "second": "Mirza" }, { "first": "Jack", "second": "Bauer" } ] }

Printing Multiple Letters With JSON ARRAY Containing Repeated Data

In order to print, e.g., multiple rows in a table, use the <repeat> tag. The elements in the array can be referenced using [*] notation.

In this case multiple PDFs will be generated. Each PDF can be unique in this case, as the repeated data can vary per json object in the array.

HTML

<h1>HELLO </h1> <table> <repeat> <tr> <td>{names[*].first} </td> <td>{names[*].second} </td> <tr> </repeat> </table>

JSON

[ { "names": [ { "first": "Farrukh", "second": "Mirza" }, { "first": "Jack", "second": "Bauer" } ] }, { "names": [ { "first": "Ethan", "second": "Hunt" }, { "first": "Michael", "second": "Corleone" }, { "first": "Json", "second": "Bourne" } ] } ]

NOTES

1. FlyingSaucer is able to pull images and place into PDF, however, the image url must be publicly accessible.

TESTED

Application Servers

This service is tested on the following application servers:

  1. Apache Tomcat version 8.0.28
  2. WildFly 10.0.0.Final
  3. Standalone deployment (Embedded Tomcat)

Web Browsers

This service is tested on the following web browsers:

  1. FireFox 47.0
  2. Google Chrome Version 51.0.2704.106 m
  3. Internet Explorer 11
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].