Sign inSign up

ronisaha/easy-pdf-rest

By ronisaha

Updated about 2 years ago

The best WeasyPrint container that exists on the web.

Image
0

950

ronisaha/easy-pdf-rest repository overview

Usage

First, you can start the container using the following command:

docker run -p 5000:5000 -v /path/to/local/templates:/data/templates -d ronisaha/easy-pdf-rest:latest

Then you can use the following command to generate the report.pdf from the official WeasyPrint sample. You can find the files in tests/resources/report.

curl  \
-F '[email protected]' \
-H X-API-KEY:key \
-F '[email protected]' \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
-F "asset[][email protected]" \
http://localhost:5000/api/v1.0/print --output report.pdf

Configuration

All configurations are set via environment variables.

NameDefaultDescription
API_KEY""Sets an API key that protects the /api/v1.0/print service from unauthorized access. The key is later compared with the header X_API_KEY. If no API_KEY is set, anyone can access the application.
BLOCKED_URL_PATTERN"^.*$"Pattern to block certain URLs. These URLs are later not allowed within resources of the print service. These resources will be ignored.
ALLOWED_URL_PATTERN"^$"Pattern to allow certain URLs. These URLs are later allowed within resources of the print service.
MAX_UPLOAD_SIZE104857600Maximum size of the upload. Default is 100MB
UPLOAD_EXTENSIONS.png,.jpg,.jpeg,.tiff,.bmp,.gif,.pdfAllowed extentions while using merge endpoint

Services

Health

Service to check if the service is still working properly.

GET /api/v1.0/health
Parameters
ParameterTypeDescription
pingstringOptional. Returns the ping in the field pong
Response
{
  "status"    : "OK",
  "weasyprint": "string",
  "timestamp" : "number",
  "pong"      : "string?"
}

The status does always contain "OK".

The weasyprint does contain the current weasyprint version.

The timestamp does contain the current timestamp of the server in milliseconds.

The pong is optional and will only be sent if the ping parameter was passed. It contains the same value that ping had.

Print

Service to print a pdf or png

POST /api/v1.0/print
Parameters
ParameterTypeRequiredDescription
htmlfile or stringSemi-RequiredHTML file to convert. html or url or report one is required. Only either url or html, report should be used.
urlfile or stringSemi-RequiredURL to convert. html or url or report one is required. Only either url or html, report should be used.
reportstringSemi-RequiredReport template name to render the html. html or url or report one is required. Only either url or html, report should be used.
datadictSemi-RequiredVariables as dictionary for rendering report template. Used along with report parameter.
optimizestringOptionalOptimize size of generated PDF. Can contain Coma seperated "images" and "fonts".
dispositionstringOptionalSet response disposition type(attachment or inline). default is inline.
file_namestringOptionalSet response disposition file_name. default is document.pdf.
passwordstringOptionalPassword protected PDF
templatestringOptionalTemplate name for the use of predefined templates.
stylefile or stringOptionalStyle to apply to the html. This should only be used if the CSS is not referenced in the html. If it is included via HTML link, it should be passed as asset. Only either style or style[] can be used.
style[]file or file[]OptionalMultiple styles to apply to the html. This should only be used if the CSS is not referenced in the html. If it is included via HTML link, it should be passed as asset. Only either style or style[] can be used.
asset[]file or file[]OptionalAssets which are referenced in the html. This can be images, CSS or fonts. The name must be 1:1 the same as used in the files.
Response

Raw output stream of with Content-Type of application/pdf also the header Content-Disposition = 'inline;filename={HTML_FILE_NAME}.pdf will be set.

Merge

Service to merge pdf and/or images

POST /api/v1.0/merge
Parameters
ParameterTypeRequiredDescription
files[]file or filesRequiredPDF or Image file
pagesstringRequiredPages definition as string or JSON
dispositionstringOptionalSet response disposition type(attachment or inline). default is inline.
file_namestringOptionalSet response disposition file_name. default is merged.pdf.
passwordstringOptionalPassword protected PDF
pages can be passed as JSON
[
  {
    "file": "file1.pdf",
    "range": "0:2"
  },
  {
    "file": "a.jpeg"
  },
  {
    "file": "file1.pdf",
    "range": "2:3"
  },
  {
    "file": "file2.pdf"
  }
]

or string like:

file1.pdf~0:2 a.jpeg file1.pdf~2:3 file2.pdf
Range Syntext
RangeDescriptionRangeDescription
:all pages.-1last page.
22just the 23rd page.:-1all but the last page.
0:3the first three pages.-2second-to-last page.
:3the first three pages.-2:last two pages.
5:from the sixth page onward.-3:-1third & second to last.
The third, "stride" or "step" number is also recognized.
RangeDescriptionRangeDescription
::20 2 4 ... to the end.3:0:-13 2 1 but not 0.
1:10:21 3 5 7 92::-12 1 0.
::-1all pages in reverse order.
Response

Raw output stream of with Content-Type of application/pdf also the header Content-Disposition = 'inline;filename=merged.pdf will be set.

Tag summary

Content type

Image

Digest

sha256:e78f8195a

Size

408.3 MB

Last updated

about 2 years ago

docker pull ronisaha/easy-pdf-rest