Giter Site home page Giter Site logo

aws-lambda-wkhtmltopdf's Introduction

aws-lambda-wkhtmltopdf

Convert HTML to PDF using Webkit (QtWebKit) on AWS Lambda

Creating function under your AWS account

  1. Log into your AWS account
  2. Under Lambda, click Create a Lambda function button
  3. When being asked to select blueprint, do not select any and click Skip button instead
  4. Do not configure any triggers and simply click Next button
  5. Enter name of your function (e.g. "html-to-pdf"), then select runtime Node.js 4.3
  6. In Code entry type change Edit code inline to Upload a .ZIP file
  7. Click Upload button to upload this project as zip archive.
  8. Select lambda_basic_execution role (this function doesn't need access to any part of your AWS account)
  9. Increase timeout from 3 seconds to 10 seconds or more.
  10. Click Next button
  11. Review your configuration and click Create function button

Testing function

Click Actions button, then click Configure test event and paste following json input:

{
    "html_base64" : "PGJvZHk+SGVsbG8gd29ybGQ8L2JvZHk+"
}

html_base64 parameter represents encoded <body>Hello world</body>

Then click Save and test button. If your function is working correctly, you should receive following output:

{
  "pdf_base64": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/KQovQ3JlYXRvciAo/v8AdwBrAGgAdABtAGwAdABvAHAAZABmACAAMAAuADEAMgAuADMALQBkAGUAdgAtADcAOQBmAGYANQAxAGUpCi9Qcm9kdWNlciAo/v8AUQB0ACAANAAuADgALgA3KQovQ3JlYXRpb25EYXRlIChEOjIwMTYxMDAxMDQzMDU5WikKPj4KZW5kb2JqCjMgMCBvYmoKPDwKL1R5cGUgL0V4dEdTdGF0ZQovU0EgdHJ1ZQovU00gMC4wMgovY2EgMS4wCi9DQSAxLjAKL0FJUyBmYWxzZQovU01hc2sgL05vbmU+PgplbmRvYmoKNCAwIG9iagpbL1BhdHRlcm4gL0RldmljZVJHQl0KZW5kb2JqCjcgMCBvYmoKPDwKL1R5cGUgL0NhdGFsb2cKL1BhZ2VzIDIgMCBSCj4+CmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlIC9QYWdlCi9QYXJlbnQgMiAwIFIKL0NvbnRlbnRzIDggMCBSCi9SZXNvdXJjZ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p0cmFpbGVyCjw8Ci9TaXplIDE3Ci9JbmZvIDEgMCBSCi9Sb290IDcgMCBSCj4+CnN0YXJ0eHJlZgozMjQ2CiUlRU9GCg=="
}

The function will respond with PDF encoded in Base64

Options

There are many options available to wkhtmltopdf. All of the command line options are supported as documented on the page linked to above. The options are camelCased instead-of-dashed as in the command line tool. Note that options that do not have values, must be specified as a boolean, e.g. debugJavascript: true

{
    "html_base64" : "PGJvZHk+SGVsbG8gd29ybGQ8L2JvZHk+",
    "options":
    {
        "pageSize": "letter"
    }
}

aws-lambda-wkhtmltopdf's People

Contributors

jblotus avatar lubos avatar ryanmagnusson 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

aws-lambda-wkhtmltopdf's Issues

Not working

I tried as per your documentation, but it is not working
{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:417:25)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)"
]
}

Some help would be appreciated.

How to test with a POST request?

The deployment worked great but I am having troubles by testing it with POST requests.
image

I've added a API gateway that enables requests but no results.
image

Please heelp 😢

compiling node_modules

Hello! thank you so much for this function, it works really well and we're about to deploy it on production.
However I need an Html to Excel and since my search has proven to be empty
I decided to implement a function that basically replicates what you do here with excels instead pdfs

but I have mental conflicts when it comes to where to compile my node_modules (I genenerally use ubuntu, but I get the feeling that those node_modules won't work)
could you give me a pointer on where to develop or where to clone/compile?

any information would be appreciated, thank you in advance

UTF-8 doesn't seem to be working

Hey there, I'm using this Lambda script, honestly it's awesome and you've done a great job @jblotus .

However there's one thing I can't get working which is UTF-8 support. As you can see below emoji fails to render (not a huge issue) but also even a £ sign doesn't appear to work properly which makes me think it's using the most basic of text encoding. $ works.

screen shot 2018-09-04 at 17 39 45

screen shot 2018-09-04 at 17 42 18

Here's the code I used to generate the base64...

const buffer = new Buffer('£', 'utf8');
const base64 = buffer.toString('base64');

Any idea what's happened? Is this something I can fix with a config option?

Thanks in advance

Error handling?

Hi there - this is an awesome project.

I am having some issues however, like my pdfs can not download any external js/css or images. I am trying to figure out how to debug this situation. Are there some callbacks that could be implemented in the handler?

Thanks

Problem when unzipping and zipping again

Hello,

When I follow your steps, the project works as intended but I need to modify the index.js to add some logic on it.

If I unzip the project and zip it again (without modifying a single line of code) it doesn't work anymore.

memStream.read() in line 9 becomes null, so when it does memStream.read().toString() it throws an error.

What can I do to add more logic to the process?

Mobile Browsers Issue

This works fine for me on a desktop browser but on a mobile browser the file is downloaded but not recognized as a .pdf. When I open the downloaded file in chrome it just has the request body contents as such:

%PDF-1.4
1 0 obj
<<
/Title (��)
/Creator (��wkhtmltopdf 0.12.3-dev-79ff51e)
/Producer (��Qt 4.8.7)
/CreationDate (D:20170505202615Z)

endobj
...

And so-on. I am completely stumped. Any ideas what could be causing this behavior on mobile browsers?

Thanks for the help!

Brandon Culpepper

Cannot read property 'toString' of null

START RequestId: f4579180-138f-11e7-a480-b98cb89920aa Version: $LATEST
2017-03-28T08:24:26.695Z f4579180-138f-11e7-a480-b98cb89920aa TypeError: Cannot read property 'toString' of null
at Response. (/var/task/index.js:27:45)
at Request. (/var/task/node_modules/aws-sdk/lib/request.js:360:18)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:673:14)
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request. (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
at Request. (/var/task/node_modules/aws-sdk/lib/request.js:675:12)
END RequestId: f4579180-138f-11e7-a480-b98cb89920aa
REPORT RequestId: f4579180-138f-11e7-a480-b98cb89920aa Duration: 326.80 ms Billed Duration: 400 ms Memory Size: 512 MB Max Memory Used: 28 MB
RequestId: f4579180-138f-11e7-a480-b98cb89920aa Process exited before completing request

Exported PDF file has error.

Dear Lubos,

I have tried to apply your source code to AWS Lambda.
I could execute lambda function successfully, but the the exported PDF file has error in format.
Adobe Reader can't read the file.
screenshot_2
Have you ever got this issue?
If you have any info, could you share with me?

Thanks so much for your help!

"errorMessage": "Cannot find module '/var/task/index'"

Hello,

I followed your steps, but when I click on test the following error appears:

{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)"
]
}

It seems like any module is not loading correctly but I couldn't find the issue.

Thanks for your time.

How do you declare the options to lambda function to be used

Hi,
Thanks a lot for your comprehensive explanation.

I am running wkhtmltopdf with such parameters on desktop:

for f in *.html; do a=$(echo $f | cut -d '.' -f1) wkhtmltopdf --dpi 360 -s A4 -B 10 -L 5 -R 5 -T 15 $f $a.pdf rm $f done

How do I declare the sam variables to lambda?
Thanks a lot

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.