-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fc5414
commit dae8bdf
Showing
12 changed files
with
200 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Joi from 'joi' | ||
|
||
export const uriWithSpaces = ( | ||
value: string, | ||
helpers: Joi.CustomHelpers<string> | ||
) => { | ||
try { | ||
new URL(value) | ||
return value | ||
} catch (error) { | ||
return helpers.error('string.uri') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export const RegexContentTypeImage = /^image\// | ||
export const RegexContentTypeDoc = | ||
/vnd.openxmlformats-officedocument.wordprocessingml.document/ | ||
export const RegexContentTypeAllow = | ||
/vnd.openxmlformats-officedocument.wordprocessingml.document|image/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Docxtemplater from 'docxtemplater' | ||
import PizZip from 'pizzip' | ||
import error from './error' | ||
import statusCode from './statusCode' | ||
|
||
class ReplaceDoc { | ||
constructor() {} | ||
|
||
public static ReplaceDoc(content: string, data: object) { | ||
try { | ||
const zip = new PizZip(content) | ||
|
||
// This will parse the template, and will throw an error if the template is | ||
// invalid, for example, if the template is "{user" (no closing tag) | ||
const doc = new Docxtemplater(zip, { | ||
paragraphLoop: true, | ||
linebreaks: true, | ||
}) | ||
|
||
// Render the document (Replace {first_name} by John, {last_name} by Doe, ...) | ||
doc.render(data) | ||
|
||
// Get the zip document and generate it as a nodebuffer | ||
const buf = doc.getZip().generate({ | ||
type: 'nodebuffer', | ||
// compression: DEFLATE adds a compression step. | ||
// For a 50MB output document, expect 500ms additional CPU time | ||
compression: 'DEFLATE', | ||
}) | ||
|
||
return buf | ||
} catch (err: any) { | ||
throw new error(statusCode.BAD_REQUEST, err.message) | ||
} | ||
} | ||
} | ||
|
||
export default ReplaceDoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters