GithubHelp home page GithubHelp logo

baristalabs / skinny-html2pdf Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 5.0 68 KB

Dockerified html2pdf Microservice built with dotnetcore

C# 100.00%
microservice docker html2pdf dotnet-core aspnet-core headless-chrome

skinny-html2pdf's Introduction

Skinny Html2PDF

Skinny Html2PDF is a .Net Core 2.1 based microservice that converts Html to PDF using chrome-dev-tools-runtime to automate headless chrome.

This project also contains a dockerfile that will build and run the microservice in a minimal environment.

While perhaps interesting in itself, some effort was made to make this stack 'skinny':

  • Built using .Net Core 2.1 (Preview) (2.1 is skinner than 2.0)
  • Docker image based on Alpine Linux (Alpine is skinner than standard debian)
  • TODO: Tree Trimming tools (seems to be broken in current Alpine)
  • TODO: https://github.com/docker-slim/docker-slim

Image size without Chrome: 150mb. With Chrome: 600mb (232mb compressed) (Of course, Chrome is required, this metric is just to illustrate the base vs base + chrome image size)

Erm, so uh, while this started out as skinny html2pdf, the size with Chrome and its dependencies makes things a tad pudgy 'round the middle; Grande, if you will. Still, not bad for an entire OS + .Net Core + Chrome.

One might separate chromium from the rest of the stack and have a swarm/k8s of chrome-only containers allowing for multi-purpose use. that would be interesting. 'Browser Cloud'

Anyway, the all-in-one image is available on docker hub here: https://hub.docker.com/r/oceanswave/skinny-html2pdf/

Getting Started


Docker must be installed and running.

docker pull oceanswave/skinny-html2pdf
docker run -d -p 80:8080 --shm-size=1gb --cap-add SYS_ADMIN oceanswave/skinny-html2pdf

Once the container is running, the services will be hosted at localhost:80 and exposes the following endpoints:

http://localhost/api/html2pdf?url=https://medium.com/netflix-techblog/embracing-the-differences-inside-the-netflix-api-redesign-15fd8b3dc49d&fileName=myarticle.pdf

Parameters:

  • url: full uri of the web page to render
  • width: Width in pixels of the page to render
  • height: Height in pixels of the page to render
  • filename: content-disposition filename value
  • landscape: true to output in landscape

http://localhost/api/html2image?url=https://www.pexels.com/photo/white-and-yellow-flower-with-green-stems-36764/

Parameters:

  • url: full uri of the web page to render
  • width: Width in pixels of the page to render
  • height: Height in pixels of the page to render
  • filename: content-disposition filename value

The service also supports ad-hoc conversion of Html to PDF.

Simply POST to http://localhost/api/html2pdf with the body containing of the HTML to convert into PDF.

Parameters:

  • width: Width in pixels of the page to render
  • height: Height in pixels of the page to render
  • filename: content-disposition filename value
  • landscape: true to output in landscape mode

Hosting on Azure


Generally, follow the instructions on https://docs.microsoft.com/en-us/azure/app-service/containers/tutorial-custom-docker-image

Within the Azure Cloud Shell:

az group create --name skinnyHtml2PdfResourceGroup --location "East US 2"
az appservice plan create --name skinnyHtml2PdfServicePlan --resource-group skinnyHtml2PdfResourceGroup --sku B1 --is-linux
az webapp create --resource-group skinnyHtml2PdfResourceGroup --plan skinnyHtml2PdfServicePlan --name skinny-html2pdf --deployment-container-image-name oceanswave/skinny-html2pdf:latest

Development


VS2017 15.6+. As .Net Core 2.1 is still in development, the .Net Core 2.1 Preview SDK must be downloaded and installed.

The latest version of Chromium available for Alpine at the time of this writing is Chrome 64, so chrome-dev-tools-runtime should not be upgraded to latest.

Build using docker-compose:

git clone https://github.com/baristalabs/skinny-html2pdf
cd skinny-html2pdf
docker-compose build
docker-compose up -d

The microservice can be built without docker-compose as well.

git clone https://github.com/baristalabs/skinny-html2pdf
cd skinny-html2pdf
docker build -rm -t oceanswave/skinnyhtml2pdf:latest -f BaristaLabs.SkinnyHtml2Pdf.Web/Dockerfile .
docker run -d -p 80:8080 --shm-size=1gb --cap-add SYS_ADMIN oceanswave/skinny-html2pdf

skinny-html2pdf's People

Contributors

oceanswave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.