-
Notifications
You must be signed in to change notification settings - Fork 570
pdf() API #84
pdf() API #84
Changes from all commits
3e7e5f3
19b8b5d
72f100c
597cc35
8319877
e217b7b
7db33a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import * as AWS from 'aws-sdk' | ||
| import { Client, Command, ChromelessOptions, Cookie, CookieQuery } from '../types' | ||
| import { Client, Command, ChromelessOptions, Cookie, CookieQuery, PdfOptions } from '../types' | ||
| import * as cuid from 'cuid' | ||
| import * as fs from 'fs' | ||
| import { | ||
|
|
@@ -10,6 +10,7 @@ import { | |
| evaluate, | ||
| screenshot, | ||
| getHtml, | ||
| pdf, | ||
| type, | ||
| getValue, | ||
| scrollTo, | ||
|
|
@@ -53,6 +54,8 @@ export default class LocalRuntime { | |
| return this.returnScreenshot() | ||
| case 'returnHtml': | ||
| return this.returnHtml() | ||
| case 'returnPDF': | ||
| return this.returnPDF(command.options) | ||
| case 'returnInputValue': | ||
| return this.returnInputValue(command.selector) | ||
| case 'type': | ||
|
|
@@ -275,6 +278,34 @@ export default class LocalRuntime { | |
| return await getHtml(this.client) | ||
| } | ||
|
|
||
| // Returns the S3 url or local file path | ||
| async returnPDF(options?: PdfOptions): Promise<string> { | ||
| const data = await pdf(this.client, options) | ||
|
|
||
| // check if S3 configured | ||
| if (process.env['CHROMELESS_S3_BUCKET_NAME'] && process.env['CHROMELESS_S3_BUCKET_URL']) { | ||
| const s3Path = `${cuid()}.pdf` | ||
| const s3 = new AWS.S3() | ||
| await s3.putObject({ | ||
| Bucket: process.env['CHROMELESS_S3_BUCKET_NAME'], | ||
| Key: s3Path, | ||
| ContentType: 'application/pdf', | ||
| ACL: 'public-read', | ||
| Body: new Buffer(data, 'base64'), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should also use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be addressed as part of #113. |
||
| }).promise() | ||
|
|
||
| return `https://${process.env['CHROMELESS_S3_BUCKET_URL']}/${s3Path}` | ||
| } | ||
|
|
||
| // write to `/tmp` instead | ||
| else { | ||
| const filePath = `/tmp/${cuid()}.pdf` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should look into using a module that helps with tmp folders. This file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With node 4 and above os.tmpdir() should do the trick.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be addressed as part of #113. |
||
| fs.writeFileSync(filePath, Buffer.from(data, 'base64')) | ||
|
|
||
| return filePath | ||
| } | ||
| } | ||
|
|
||
| private log(msg: string): void { | ||
| if (this.chromelessOptions.debug) { | ||
| console.log(msg) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably this should be configurable as well with an env variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be addressed as part of #113.