Skip to content

Commit

Permalink
use new format to specify inline pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrmatos committed Jun 4, 2019
1 parent cd67057 commit e8c7c46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,24 @@ function execute (reporter) {
reporter.logger.debug(`static-pdf is using asset ${pdfAssetPath}`, req)

pdfAsset = assetEntity.content
} else if (req.template.staticPdf.rawContent != null) {
if (Buffer.isBuffer(req.template.staticPdf.rawContent)) {
pdfAsset = req.template.staticPdf.rawContent
} else if (typeof req.template.staticPdf.rawContent === 'string') {
pdfAsset = Buffer.from(req.template.staticPdf.rawContent, 'base64')
} else if (req.template.staticPdf.pdfAsset != null) {
if (Buffer.isBuffer(req.template.staticPdf.pdfAsset.content)) {
pdfAsset = req.template.staticPdf.pdfAsset.content
} else {
throw reporter.createError(`Invalid value for req.template.staticPdf.rawContent, specify either a base64 string or a buffer`, {
statusCode: 400
})
if (!req.template.staticPdf.pdfAsset.encoding) {
throw reporter.createError(`Missing value for req.template.staticPdf.pdfAsset.encoding, encoding is required`, {
statusCode: 400
})
}

pdfAsset = Buffer.from(req.template.staticPdf.pdfAsset.content, req.template.staticPdf.pdfAsset.encoding)
}

reporter.logger.debug(`static-pdf is using inline content`, req)
}

if (!pdfAsset) {
throw reporter.createError(`Source PDF asset was not specified, specify either req.template.staticPdf.pdfAssetShortid or req.template.staticPdf.rawContent`, {
throw reporter.createError(`Source PDF asset was not specified, specify either req.template.staticPdf.pdfAssetShortid or req.template.staticPdf.pdfAsset`, {
statusCode: 400
})
}
Expand Down
24 changes: 22 additions & 2 deletions test/staticPdfTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe('static-pdf', () => {
engine: 'none',
recipe: 'static-pdf',
staticPdf: {
rawContent: fs.readFileSync(path.join(__dirname, 'static.pdf'))
pdfAsset: {
content: fs.readFileSync(path.join(__dirname, 'static.pdf'))
}
}
}
})
Expand All @@ -43,7 +45,10 @@ describe('static-pdf', () => {
engine: 'none',
recipe: 'static-pdf',
staticPdf: {
rawContent: fs.readFileSync(path.join(__dirname, 'static.pdf')).toString('base64')
pdfAsset: {
content: fs.readFileSync(path.join(__dirname, 'static.pdf')).toString('base64'),
encoding: 'base64'
}
}
}
})
Expand Down Expand Up @@ -72,6 +77,21 @@ describe('static-pdf', () => {
should(result.content.toString().includes('PDF')).be.true()
})

it('should throw error when inline asset does not specify encoding', async () => {
return should(reporter.render({
template: {
content: 'Hello',
engine: 'none',
recipe: 'static-pdf',
staticPdf: {
pdfAsset: {
content: fs.readFileSync(path.join(__dirname, 'static.pdf')).toString('base64')
}
}
}
})).be.rejectedWith(/encoding is required/)
})

it('should throw error when asset reference is not PDF', async () => {
await reporter.documentStore.collection('assets').insert({
name: 'img.png',
Expand Down

0 comments on commit e8c7c46

Please sign in to comment.