Created
October 19, 2014 16:42
-
-
Save cunneen/5547c9b6016bbf7f5124 to your computer and use it in GitHub Desktop.
This is the rough outline of a regular (weekly) job that fills in a PDF form and will email it. There are 3 aspects:
1. [percolate studio's meteor cron](https://github.com/percolatestudio/meteor-synced-cron)
2. PDFtk installed
3. [meteor pdftk wrapper](https://github.com/pascoual/meteor-pdftk) The sample PDF with acrofields for this demo can be …
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
var meteor_root = Npm.require('fs').realpathSync(process.cwd() + '/../'); | |
var application_root = Npm.require('fs').realpathSync(meteor_root + '/../'); | |
// if running on dev mode | |
var fs = Npm.require('fs'); | |
if (Npm.require('path').basename(fs.realpathSync(meteor_root + '/../../../')) == '.meteor') { | |
application_root = fs.realpathSync(meteor_root + '/../../../../'); | |
} | |
var assets_folder = meteor_root + '/server/assets/'; | |
SyncedCron.add({ | |
name: 'Crunch some important numbers for the marketing department', | |
schedule: function (parser) { | |
// parser is a later.parse object | |
return parser.text('every 1 minute'); | |
}, | |
job: function () { | |
console.log('crunching numbers') | |
} | |
}); | |
Meteor.startup(function () { | |
// code to run on server at startup | |
SyncedCron.start(); | |
// Stop jobs after 15 seconds | |
// Meteor.setTimeout(function () { | |
// SyncedCron.stop(); | |
// }, 15 * 1000); | |
// | |
//TODO: get rid of this. | |
// try and generate a PDF | |
console.log("meteor_root:"); | |
console.log(meteor_root); | |
console.log("application_root"); | |
console.log(application_root); | |
console.log("assets_folder"); | |
console.log(assets_folder); | |
createFilledForm(); | |
}); | |
// example to create a stamped pdf is: | |
//PDFTK.stamp('.../sample.pdf', | |
// '.../pdfStamp.pdf', | |
// '.../result.pdf', | |
// function (err, buffer) { | |
// if (err) | |
// console.log(err); | |
// }); | |
function getXFDF() { | |
//TODO: make this dynamic | |
data = '\'<?xml version="1+0" encoding="UTF-8"?>\ | |
<xfdf xmlns="http://ns+adobe+com/xfdf/" xml:space="preserve">\ | |
<fields>\ | |
<field name="Text2">\ | |
<value>Value2</value>\ | |
</field>\ | |
<field name="Text3">\ | |
<value>Value3</value>\ | |
</field>\ | |
<field name="Text4">\ | |
<value>Value4</value>\ | |
</field>\ | |
<field name="Text5">\ | |
<value>Value5</value>\ | |
</field>\ | |
</fields>\ | |
</xfdf>\''; | |
//<ids original="'+md5(file)+'" modified="'+time()+'" />'+"\n"+ | |
return data; | |
} | |
function fillFormWithXFDF(xfdffile) { | |
PDFTK.fillForm(assets_folder + 'packages/prestart/server/sample.pdf', | |
xfdffile, | |
function (err, buffer) { | |
if (err) | |
console.log(err); | |
else { | |
console.log(buffer); | |
console.log("=== I think that was a PDF ==="); | |
fs.writeFile(assets_folder + 'packages/prestart/server/results.pdf', buffer, 'binary', function (err) { | |
if (err) { | |
throw (new Meteor.Error(500, 'Failed to save file.', err)); | |
} else { | |
console.log('results.pdf was saved.'); | |
} | |
}); | |
} | |
}); | |
} | |
function createFilledForm() { | |
// TODO Add file existance checks, etc... | |
var filename = assets_folder + 'packages/prestart/server/myFile.tmp'; | |
fs.writeFile(filename, getXFDF(), 'UTF-8', function (err) { | |
if (err) { | |
throw (new Meteor.Error(500, 'Failed to save file.', err)); | |
} else { | |
console.log('The file ' + filename + ' (' + 'UTF-8' + ') was saved.'); | |
fillFormWithXFDF(filename); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment