Coder Social home page Coder Social logo

falconandy / docker-wkhtmltopdf-service Goto Github PK

View Code? Open in Web Editor NEW

This project forked from motorsportreg/docker-wkhtmltopdf-service

0.0 2.0 0.0 27 KB

A dockerized webservice that uses webkit to convert HTML to PDF or image

License: MIT License

Go 82.42% Shell 1.56% Dockerfile 16.02%

docker-wkhtmltopdf-service's Introduction

wkhtmltopdf as a web service

A dockerized webservice written in Go that uses wkhtmltopdf to convert HTML into documents (images or pdf files).

Build & Run

HTTP: docker build -t wkhtmltopdf . && docker run --rm -it -p 3000:3000 -e SECURE=false pukkasoftware/wkhtmltopdf

HTTPS: docker build -t wkhtmltopdf . && docker run --rm -it -p 3000:3000 -v $(pwd)/app/ssl:/app/ssl -e AUTHENTICATION=username:password pukkasoftware/wkhtmltopdf

For security purposes, we do not embed the SSL certificates in the image. Instead, mount a volume containing a cert.pem and key.pem to /app/ssl

Optionally enable HTTP basic auth using the AUTHENTICATION environment variable. Failure to auth will result in a 401 error.

Usage

The service listens on port 3000 for POST requests on the root path (/). Any other method returns a 405 not allowed status. Any other path returns a 404 not found status.

The body should contain a JSON-encoded object containing the following parameters:

  • output: The type of document to generate, can be either jpg, png or pdf. Defauts to pdf if not specified. Depending on the output type the appropriate binary is called.

  • requests: An array of documents to be converted, each of which has the following parameters:

    One of:

    • content: The HTML/CSS to be converted.
    • url: The URL of the page to fetch and convert.

    And:

    • options: A list of key-value arguments that are passed on to the appropriate wkhtmltopdf binary. Boolean values are interpreted as flag arguments (e.g.: --greyscale).
    • cookies: A list of key-value arguments that are passed on to the appropriate wkhtmltopdf binary as separate cookie arguments.

Example: posting the following JSON:

{
  "output": "pdf",
  "requests": [
    {
      "content": "<html><body><h1>Google.com<\/h1><p>The HTML that would have been fetched<\/p><\/body><\/html>",
      "options": {
        "margin-bottom": "1cm",
        "orientation": "Landscape"
      },
      "cookies": {
        "foo": "bar",
        "baz": "foo"
      }
    },
    {
      "url": "google.com",
      "options": {
        "margin-bottom": "1cm",
        "orientation": "Landscape",
        "grayscale": true
      },
      "cookies": {
        "foo": "bar",
        "baz": "foo"
      }
    },
    {
      "content": "<html><body><h1>Yahoo.com<\/h1><p>The HTML that would have been fetched<\/p><\/body><\/html>",
      "options": {
        "margin-bottom": "1cm",
        "orientation": "Landscape",
        "grayscale": true
      },
      "cookies": {
        "foo": "bar",
        "baz": "foo"
      }
    }
  ]
}

will have the effect of the following command-line being executed on the server:

echo "content" | /usr/local/bin/wkhtmltopdf --margin-bottom 1cm --orientation Landscape --grayscale --cookie foo bar --cookie baz foo - -
/usr/local/bin/wkhtmltopdf --margin-bottom 1cm --orientation Landscape --cookie foo bar --cookie baz foo google.com -
echo "content" | /usr/local/bin/wkhtmltopdf --margin-bottom 1cm --orientation Landscape --grayscale --cookie foo bar --cookie baz foo - -

The - at the end of the command-line is so that the document contents are redirected to stdout so we can in turn redirect it to the web server's response stream.

When using jpg or png output, the set of options you can pass are actually more limited. Please check wkhtmltopdf usage docs or rather just use wkhtmltopdf --help or wkhtmltoimage --help to get help on the available command-line arguments. jpg and png only process the first element of the requests array.

You can also pass in the HTML/CSS directly like so:

{"output": "pdf",
 "requests": [{
  "content": "<html><body><h1>My Web Page<\/h1><p>The HTML that would have been fetched<\/p><\/body><\/html>",
  "options": {
    "margin-bottom": "1cm",
    "orientation": "Landscape",
    "grayscale": true
  },
  "cookies": {
    "foo": "bar",
    "baz": "foo"
  }
}]
}

Known limitations / TODOs

There are challenges with converting raw HTML when you have relative links. There are known workarounds that we haven't yet tried (see this stack overflow question for more information).

The server currently does not check if the arguments are valid, just passes them on to the wkhtmltopdf and wkhtmltoimage binaries. If you pass a wrong argument you don't currently get an error, just an empty image or pdf back.

License

This code is released under the MIT License.

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.