Giter Site home page Giter Site logo

zeplin / zeplin-html-to-pdf Goto Github PK

View Code? Open in Web Editor NEW
123.0 29.0 49.0 16.45 MB

AWS Lambda function that converts HTML pages to PDF documents

License: MIT License

JavaScript 100.00%
html-to-pdf wkhtmltopdf aws-lambda nodejs zeplin pdf-document html pdf

zeplin-html-to-pdf's Introduction

Zeplin HTML to PDF Lambda

This is an AWS Lambda function that converts HTML pages to PDF documents using wkhtmltopdf (0.12.4), it implements a simple interface to read and HTML input and output PDF content.

WARNING: You must use v2.x.x tag if you are using Node.js runtime >=10.x in AWS Lambda.

Input

Input event to this function has the following structure:

{
    "html": "<!DOCTYPE html><html><head><title>HTML doc</title></head><body>Content</body></html>"
}

Output

It yields a response in the following format:

{
  "data": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7..."
}

data is base64 encoding of the converted PDF file.

Test in local environment

The function can be tested locally using AWS SAM CLI. You can change contents of events/example-event.json or you can create a new file which you will give sam as an event parameter.

sam local invoke "HtmlToPdf" -e events/example-event.json

Deploying to AWS

There are two ways in which these functions can be deployed to AWS.

1 - Check our npm run deploy:dev and npm run deploy:prod commands in package.json and change it according to your needs. Do not forget to add environment variables (you can find it under template.yml) to your lambda function in aws lambda edit page or running lamba update-function-configuration command. Be sure to create the following environment variable in Lambda: FONTCONFIG_PATH=/var/task/fonts

2 - Check out template.yml file and edit according to your needs then use sam deploy.

zeplin-html-to-pdf's People

Contributors

ceylanismail avatar dirtybit avatar dorukgunes avatar eren-zeplin avatar melsener avatar micah500 avatar negomi avatar okan-cakmak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zeplin-html-to-pdf's Issues

wkhtmltopdf process exited with code 126

Hi guys,

I am getting this error when deployed on Lambda:

{
"errorMessage": "[ServerError] Internal server error\nError: wkhtmltopdf process exited with code 126\n at ChildProcess.proc.on.on.code (/var/task/utils/wkhtmltopdf.js:12:24)\n at emitTwo (events.js:106:13)\n at ChildProcess.emit (events.js:191:7)\n at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)"
}

Do you have any hint?
Thanks!

wkhtmltopdf process exited with code 126

Hi guys,

I am getting this error when deployed on Lambda:

Error: wkhtmltopdf process exited with code 126
at ChildProcess. (file:///var/task/utils/wkhtmltopdf.js:12:24)
at ChildProcess.emit (node:events:517:28)
at ChildProcess._handle.onexit (node:internal/child_process:292:12)

Do you have any hint?
Thanks!

I receive empty PDF

Hi.

When I download the repo and upload to the AWS Lambda function, I get an empty PDF file as the base64 encoded string. Please help me to resolve this issue. I am using Lambda Node 4.3 as runtime.

Thank you.

Page Dimensions

It creates a PDF that is 7.5x11.5 inches. This only became a problem when we started trying to use this tool to create printable sticker-labels. There is no good way to match it up with the labels because the pages are different. Please let me know what can be done to fix it. Thanks!

NPM install reference to ssh://[email protected]/zeplin/eslint-config-zeplin.git

I believe https://github.com/zeplin/eslint-config-zeplin.git was renamed to https://github.com/zeplin/eslint-config.git

package.json: "eslint-config-zeplin": "git+ssh://[email protected]/zeplin/eslint-config-zeplin.git#v1.x.x"

21 verbose stack Error: exited with error code: 128
21 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/util/finished.js:12:19)
21 verbose stack     at ChildProcess.emit (events.js:182:13)
21 verbose stack     at maybeClose (internal/child_process.js:962:16)
21 verbose stack     at Socket.stream.socket.on (internal/child_process.js:381:11)
21 verbose stack     at Socket.emit (events.js:182:13)
21 verbose stack     at Pipe._handle.close (net.js:606:12)
22 verbose cwd /Users/eric/Work/zeplin-html-to-pdf
23 verbose Darwin 17.7.0
24 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
25 verbose node v10.13.0
26 verbose npm  v6.4.1
27 error Error while executing:
27 error /usr/bin/git ls-remote -h -t ssh://[email protected]/zeplin/eslint-config-zeplin.git
27 error
27 error [email protected]: Permission denied (publickey).
27 error fatal: Could not read from remote repository.
27 error
27 error Please make sure you have the correct access rights
27 error and the repository exists.
27 error
27 error exited with error code: 128
28 verbose exit [ 1, true ]

Performance Questions

Hi. First of all, thanks for the code. I appreciate your work on this project. I have this running on aws Lambda and everything seems to be working great except for 2 things.

  1. I'm seeing 2.5 second processing with very basic 11K PDF's.
  2. If the PDF contains links to large images (2 images ~4M each in my case) the PDF will fail to be generated and throws an error.

My questions are:
Is this the normal processing speed? Is there anything I can do to improve the processing speed?
Do you have any idea why the PDF fails to generate with large images?

Upgrade to wkhtmltopdf v0.12.5

If I am not mistaken, the binary for wkhtmltopdf included in lib corresponds to version 0.12.4 (is this documented somewhere?).

What would it take to upgrade to 0.12.5 (the latest published version)? The packaging strategy seems to have changed between these two versions, and it is not clear which binary (if any) would work with the Lambda environment. I would guess wkhtmltox-0.12.5-1.centos7.x86_64.rpm is the closest one, but I have not been able to get it to work.

This post provides some guidance, but it is unclear how to get it working inside the Lambda.

Validation error: Missing field 'html'

Wed Nov 07 15:29:06 UTC 2018 : Lambda execution failed with status 200 due to customer function error: [BadRequest] Validation error: Missing field 'html'.. Lambda request id: dcdd1b72-e2a1-11e8-aef9-5b3e80f6ba37 Wed Nov 07 15:29:06 UTC 2018 : Method completed with status: 502

Doing a console.log("event: %j", event); I get this response:

{
    "resource": "/htmlToPdf",
    "path": "/htmlToPdf",
    "httpMethod": "POST",
    "headers": {
        "Content-Type": " application/json"
    },
    "multiValueHeaders": {
        "Content-Type": [
            " application/json"
        ]
    },
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "path": "/htmlToPdf",
        "accountId": "00000000",
        "resourceId": "xxxxxx",
        "stage": "test-invoke-stage",
        "domainPrefix": "testPrefix",
        "requestId": "dcdc7ebb-e2a1-11e8-9cdf-d333c8045e22",
        "identity": {
            "cognitoIdentityPoolId": null,
            "cognitoIdentityId": null,
            "apiKey": "test-invoke-api-key",
            "cognitoAuthenticationType": null,
            "userArn": "arn:aws:iam::0000000000:user/eric",
            "apiKeyId": "test-invoke-api-key-id",
            "userAgent": "aws-internal/3 aws-sdk-java/1.11.432 Linux/4.9.124-0.1.ac.198.71.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.181-b13 java/1.8.0_181",
            "accountId": "xxx",
            "caller": "xxx",
            "sourceIp": "test-invoke-source-ip",
            "accessKey": "xxx",
            "cognitoAuthenticationProvider": null,
            "user": "xxx"
        },
        "domainName": "testPrefix.testDomainName",
        "resourcePath": "/htmlToPdf",
        "httpMethod": "POST",
        "extendedRequestId": "xxxxxxx",
        "apiId": "aaaaaaaa"
    },
    "body": "{\n  \"html\": \"<!DOCTYPE html><html><head><title>HTML doc</title></head><body>Content<body></html>\"\n}",
    "isBase64Encoded": false
}

I'm new to both Lambda and nodejs - I'm currently setup with:

aws lambda create-function --region eu-central-1 --function-name 'htmlToPdf' --role 'arn:aws:iam::00000000:role/AWSLambdaBasicExecutionRole' --runtime 'nodejs6.10' --handler 'index.handler' --zip-file fileb://`pwd`/package.zip`

Is the node.js runtime wrong? Some other setting in AWS Lambda?

Error: write EPIPE

When I am testing on my local machine I am getting the following error.
Any clue how this can be resolved?

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at exports._errnoException (util.js:911:11)
    at WriteWrap.afterWrite (net.js:785:14)

The following is the command

node ./local/index.js ../test.html test.pdf

Cannot read property 'toString' of null

When I tested with the string recommended, receive the following message:

{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Module.require (module.js:596:17)",
"require (internal/module.js:11:18)"
]
}

When options are set getting Error: wkhtmltopdf process exited with code 1

The following is the payload and the options that are set

["--page-size Letter","--orientation Portrait","--margin-bottom 1","--margin-top 1","--margin-right 1","--margin-left 1"]

And the code is changed to send these options as second param..

wkhtmltopdf(html, event.options)
        .then(function(buffer) {
            callback(null, {
                data: buffer.toString("base64")
            });
        }).catch(function(error) {
            callback(errorUtil.createErrorResponse(500, "Internal server error", error));
        });

And I am getting the following error when executed in Lamda env.
{"errorMessage":"[ServerError] Internal server error\nError: wkhtmltopdf process exited with code 1\n at ChildProcess.<anonymous> (/var/task/utils/wkhtmltopdf.js:16:24)\n at emitTwo (events.js:87:13)\n at ChildProcess.emit (events.js:172:7)\n at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)"}

Thank in advance.

Incorrect Characters in Output PDF

Hi there,

Although HTML is accepted by the function, once decoding the output data no text appears, instead I just see black squares where the characters should be. It's like I've got the character encoding wrong or something! This running in AWS Lambda on Node 12.x.

Here's an example of the input:
{ "html": "<!DOCTYPE html><html><head><title>HTML Document</title></head><body>Content <h1>title here</h1> More info here</body></html>" }

And the output:
{ "data": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/AEgAVABNAEwAIABEAG8AYwB1AG0AZQBuAHQpCi9DcmVhdG9yICj+/wB3AGsAaAB0AG0AbAB0AG8AcABkAGYAIAAwAC4AMQAyAC4AMwAtAGQAZQB2AC0ANwA5AGYAZgA1ADEAZSkKL1Byb2R1Y2VyICj+/wBRAHQAIAA0AC4AOAAuADcpCi9DcmVhdGlvbkRhdG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jcWHut342p7Yb5LkNnAyQo3FhTnNspQKbe/HCZuBkgByNs/nv32affvN5ud97rs9Lt2EzPwEZkLYrCmVuZHN0cmVhbQplbmRvYmoKMTIgMCBvYmoKNzc1CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlIC9QYWdlcwovS2lkcyAKWwo1IDAgUgpdCi9Db3VudCAxCi9Qcm9jU2V0IFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQ10KPj4KZW5kb2JqCnhyZWYKMCAxNQowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMTg2MiAwMDAwMCBuIAowMDAwMDAwMjA3IDAwMDAwIG4gCjAwMDAwMDAzMDIgMDAwMDAgbiAKMDAwMDAwMDY5NCAwMDAwMCBuIAowMDAwMDAwMzM5IDAwMDAwIG4gCjAwMDAwMDAzOTAgMDAwMDAgbiAKMDAwMDAwMDUzMiAwMDAwMCBuIAowMDAwMDAwNDMxIDAwMDAwIG4gCjAwMDAwMDA1OTIgMDAwMDAgbiAKMDAwMDAwMDk5MSAwMDAwMCBuIAowMDAwMDAxODQyIDAwMDAwIG4gCjAwMDAwMDA4MTUgMDAwMDAgbiAKMDAwMDAwMDk3MSAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDE1Ci9JbmZvIDEgMCBSCi9Sb290IDEwIDAgUgo+PgpzdGFydHhyZWYKMTk2MAolJUVPRgo=" }

Am I missing something super obvious? Any help is greatly appreciated.

Micah

download.pdf

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.