Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/diomalta/html-pdf
This repository hosts a project that offers a streamlined method for converting HTML to PDF using Google Chrome.
https://github.com/diomalta/html-pdf
chrome html pdf pdf-generation
Last synced: about 1 month ago
JSON representation
This repository hosts a project that offers a streamlined method for converting HTML to PDF using Google Chrome.
- Host: GitHub
- URL: https://github.com/diomalta/html-pdf
- Owner: diomalta
- License: mit
- Created: 2023-12-25T14:18:57.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-08-01T02:43:44.000Z (4 months ago)
- Last Synced: 2024-09-29T22:53:37.255Z (about 2 months ago)
- Topics: chrome, html, pdf, pdf-generation
- Language: TypeScript
- Homepage:
- Size: 93.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE.txt
Awesome Lists containing this project
README
# HTML to PDF
This project provides an easy way to convert HTML to PDF using Google Chrome. It is designed to be used in a Docker environment. It is fast because it reuses the same instance of Chrome.
## Prerequisites
[Google Chrome installed on the Docker image](#notes)
## Installation
```bash
pnpm add @diomalta/html-pdf
```## Usage
First, import the `init` functions when initializing your application:
```javascript
import { init, convertHtmlToPdf } from '@diomalta/html-pdf';
import Application from '@diomalta/http-server';// Initialize the HTML to PDF service
init({ port: 39489 });// Create a new Application instance
const app = new Application({ port: 3000 });// Define a GET route to convert HTML to PDF
app.get('/html-to-pdf', (req, res) => {
const html = 'Hello, world!
';const outputOptions = {
type: 'base64',
};const base64Pdf = await convertHtmlToPdf({ html, outputOptions });
res.status(200).send(base64Pdf);
});// Start the server on port 3000
app.listen({ port: 3000 });
```## Notes
Make sure that Google Chrome is installed on your Docker image. If it is not, you can install it by adding the following lines to your Dockerfile:
```dockerfile
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -y google-chrome-stable
```This will add the Google Chrome repository to the APT sources list, update the package list, and install Google Chrome.
## License
This project is licensed under the MIT License - see the [LICENSE](MIT-LICENSE.txt) file for details.