diff --git a/README.md b/README.md index 4d744a7..da1e24f 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ For deployment ``` sls deploy ``` + +Manually set [binary media types](https://s.natalian.org/2017-04-30/binary-support.png) for the end point. + diff --git a/example/example.html b/example/example.html index 43ee6b0..49f6d2d 100644 --- a/example/example.html +++ b/example/example.html @@ -1,44 +1,43 @@ -! - - - Convert HTML to PDF Example - - - - - - -
-
-
-
- - -
-
- -
-
-
-
- - + + + + + + + +Serverless URL to PDF generator + + + + +
+ + +
+ + + + + diff --git a/example/index.html b/example/index.html new file mode 100644 index 0000000..d840ab7 --- /dev/null +++ b/example/index.html @@ -0,0 +1,58 @@ + + + + + + + + + + +Serverless URL to PDF generator + + +
+ + +
+ + + + +

Source code

+ +
Advertisement

If you need stateless Web kiosk software, for + showing off (Web sign or dashboard) or interacting with a Web site (school exams, tourist information), choose Webconverger!

+ + + diff --git a/example/pdf.svg b/example/pdf.svg new file mode 100644 index 0000000..27006a5 --- /dev/null +++ b/example/pdf.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/example/sync.sh b/example/sync.sh new file mode 100755 index 0000000..3fad6de --- /dev/null +++ b/example/sync.sh @@ -0,0 +1,3 @@ +aws --profile mine s3 cp --acl public-read index.html s3://webc-pdf/ +aws --profile mine s3 cp --acl public-read pdf.svg s3://webc-pdf/ +aws --profile mine cloudfront create-invalidation --distribution-id E23X77OCT994XR --invalidation-batch "{ \"Paths\": { \"Quantity\": 1, \"Items\": [ \"/*\" ] }, \"CallerReference\": \"$(date +%s)\" }" diff --git a/handler.js b/handler.js index 7880a27..9e09af2 100644 --- a/handler.js +++ b/handler.js @@ -1,46 +1,49 @@ -'use strict'; +'use strict' -const fs = require('fs'); -const path = require('path'); -const execFile = require('child_process').execFile; +const fs = require('fs') +const path = require('path') +const execFile = require('child_process').execFile module.exports.print = (event, context, callback) => { - console.log(event); - const body = JSON.parse(event.body); - const phantomjs = path.resolve('bin/phantomjs-linux'); - const rasterize = path.resolve('lib/rasterize.js'); - const outputPDF = '/tmp/output.pdf'; - const url = body.url; + console.log(event) + const body = JSON.parse(Buffer.from(event.body, 'base64').toString('utf8')) + const phantomjs = path.resolve('bin/phantomjs-linux') + const rasterize = path.resolve('lib/rasterize.js') + const outputPDF = '/tmp/output.pdf' + const url = body.url if (!url) { - const err = 'url parameter is undefined'; + const err = 'url parameter is undefined' return callback(err, { statusCode: 500, body: JSON.stringify({ 'error': err }) - }); + }) } - execFile(phantomjs, [rasterize, url, outputPDF, "A4", 0.68], (err, stdout, stderr) => { - console.log('execute phantomjs'); + execFile(phantomjs, [rasterize, url, outputPDF], (err, stdout, stderr) => { + console.log('execute phantomjs') if (err) { - console.log(err); + console.log(err) callback(err, { statusCode: 500, body: JSON.stringify({ 'error': err - }), - }); + }) + }) } - const output = fs.readFileSync(outputPDF); + const output = fs.readFileSync(outputPDF) callback(null, { statusCode: 200, headers: { 'Access-Control-Allow-Origin': '*', + 'Content-Type': 'application/pdf', + 'Content-Disposition': 'inline; filename="sample.pdf"' }, - body: output.toString('base64') - }); - }); + body: output.toString('base64'), + isBase64Encoded: true + }) + }) // Use this code if you don't use the http event with the LAMBDA-PROXY integration // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); -}; +} diff --git a/serverless.yml b/serverless.yml index de41ab4..22222c9 100644 --- a/serverless.yml +++ b/serverless.yml @@ -15,10 +15,10 @@ service: serverless-pdf provider: name: aws - runtime: nodejs4.3 + runtime: nodejs6.10 stage: stage - region: us-west-2 - profile: serverless + profile: mine + region: ap-southeast-1 functions: print: