ASP.NET Core and Playwright. provides an API to generate PDFs from HTML
1.6K
This project is a Minimal APIs application built with ASP.NET Core and Playwright. It provides an API to generate PDFs from HTML content or URLs, with additional features such as hiding elements, adding watermarks (text or image), and stamping images onto the generated PDF. The API includes Swagger documentation and supports API Key authentication. It is containerized for easy deployment using Docker.
Clone the Repository:
git clone https://github.com/neozhu/PdfGeneratorApi.git
cd PdfGeneratorApi
Restore Dependencies:
dotnet restore
Set the ApiKey in your appsettings.json file to secure your API:
{
"ApiKey": "your-api-key-here",
// ... other settings
}
Alternatively, you can set the ApiKey in the appsettings.Development.json file for development purposes:
{
"ApiKey": "your-api-key-here"
}
Note: Do not commit sensitive information like API keys to version control repositories. For production environments, consider using environment variables or a secure secrets management solution to store sensitive data.
Run the Application:
dotnet run
Access Swagger UI (in development mode):
Open your browser and navigate to http://localhost:5000/swagger to view the API documentation and interact with the API.
Build the Docker Image:
docker build -t pdfgeneratorapi .
Run the Docker Container:
docker run -d -p 5000:80 -e AapiKey=your-api-key-here pdfgeneratorapi
Access Swagger UI:
Open your browser and navigate to http://localhost:5000/swagger.
You can also run the application using Docker Compose with the following configuration:
version: '3.8'
services:
pdfgeneratorapi:
image: blazordevlab/pdfgeneratorapi:latest
container_name: pdfgeneratorapi_container
ports:
- "8980:8080"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ApiKey=your-api-key-here
restart: unless-stopped
Steps to Run with Docker Compose:
Create a docker-compose.yml File:
Save the above configuration into a file named docker-compose.yml in your project directory.
Run Docker Compose:
docker-compose up -d
Access Swagger UI:
Open your browser and navigate to http://localhost:8980/swagger to interact with the API documentation.
Note:
your-api-key-here with your actual API key.ports section matches your desired host and container ports.restart: unless-stopped policy ensures that the container restarts automatically unless you stop it manually./generate-pdfstring, optional): The web page URL to convert to PDF.string, optional): The HTML content to convert to PDF.string, optional): CSS selectors of elements to hide.string, optional): Text to use as a watermark.IFormFile, optional): Image file to use as a watermark.IFormFile, optional): Stamp image file.curl -X POST http://localhost:5000/generate-pdf \
-H "X-API-KEY: your-api-key-here" \
-F "HtmlContent=<h1>Hello, World!</h1>" \
-F "WatermarkText=Confidential" \
-F "WatermarkPosition=Center" \
-o output.pdf
curl -X POST http://localhost:5000/generate-pdf \
-H "X-API-KEY: your-api-key-here" \
-F "HtmlContent=<p>This is a sample PDF generated from HTML content.</p>" \
-o sample.pdf
curl -X POST http://localhost:5000/generate-pdf \
-H "X-API-KEY: your-api-key-here" \
-F "Url=https://www.example.com" \
-F "WatermarkText=Confidential" \
-o confidential.pdf
Uploading an image file:
curl -X POST http://localhost:5000/generate-pdf \
-H "X-API-KEY: your-api-key-here" \
-F "HtmlContent=<p>PDF with uploaded image watermark.</p>" \
-F "WatermarkImageFile=@/path/to/watermark.png" \
-o uploaded_image_watermark.pdf
curl -X POST http://localhost:5000/generate-pdf \
-H "X-API-KEY: your-api-key-here" \
-F "HtmlContent=<p>PDF with stamp image.</p>" \
-F "StampImageFile=@/path/to/watermark.png" \
-F "StampPosition=RightBottom" \
-o stamped.pdf
X-API-KEY header with a valid API key for all requests.Contributions are welcome! Please follow these steps:
Fork the Repository
Create a Feature Branch:
git checkout -b feature/YourFeature
Commit Your Changes:
git commit -m "Add your feature"
Push to the Branch:
git push origin feature/YourFeature
Create a Pull Request
This project is licensed under the MIT License.
Content type
Image
Digest
sha256:b550fb2cc…
Size
463.5 MB
Last updated
over 1 year ago
docker pull blazordevlab/pdfgeneratorapi