This microservice will convert HTML to PDF. The conversion will be done with wkhtmltopdf
- docker
$ make
curl -X POST http://localhost:8080 -H 'Content-Type: application/json' -d '{"content":"<h1>Test</h1><p>Hello world</p>","options":{ "pageSize": "letter" }}'
- content is the HTML which should be converted
- options are optional parameters
Simple to use HTML to PDF API. Just send HTML content in json body request. Make sure that html you are sending is string. You can use bootrstrap or any other design framework, but just import css file in link tag in header. Default port on which api is located is 8080, but you can change it on any other port. If you are using ReactJS, you can easily transform any component to string HTML, but you have to provide css of classes in tag.
Converting component to string HTML: `import { renderToString } from "react-dom/server";
renderToString(anyComponent)`
Now you can send this string via fetch or axios instance request. If you want to download PDF on a button click use it like this:
`const response = await pdfGenerator.post(
"/",
{
content: renderToString(
renderToString(anyComponent), //or any other HTML string
),
options: { pageSize: "letter" },
},
{
responseType: "blob", // blob will tell that we are wanting file to be downloaded, and not bite array
headers: {
Accept: "application/pdf",
},
}
);`
If you want do download preview of any html or any web page and get response in image format, you can.
curl --location --request POST 'http://localhost:8080/image' \ --header 'Content-Type: application/json' \ --data-raw '{"content":"https://google.com","options":{ "format": "png", "width": "1024" }}'
What you can send in options is: quality (default is 94: int), width (def. 1024: int), height, format (png, jpg...: string), for more info read here wkhtmltopdf. If you don't want to use options, just remove options property from json boy.
Well you can do almost anything with it. It's simple to use, only one step (one request) keeps you away from generated PDF, so simple. You can make almost any design, use imports of tailwing, bootstrap and any other design framework
I tested it with this import:
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
and it works perfectly.
Just have in mind that you can provide url and html string inside of content body parameter.
This is public domain.