Meteor jsPDF Package Error - javascript

I added js-pdf package to my Meteor app but whenever I run a simple hello world code I get an error:
Code:
var doc = new jsPDF();
doc.setFontSize(40);
doc.text(35, 25, "Paranyan loves jsPDF");
I get:
[Error] ReferenceError: Can't find variable: jsPDF
click #download-pdf (manageatas.js, line 53)
(anonymous function) (templating.js, line 120)
(anonymous function) (blaze.js, line 2205)
withCurrentView (blaze.js, line 2038)
(anonymous function) (blaze.js, line 2204)
(anonymous function) (blaze.js, line 802)
dispatch (jquery.js, line 4657)
handle (jquery.js, line 4325)
I tried with the package installed, I tried downloading the code and adding it to client side folder but I get the same error again ...
What's wrong ?

I've had a load of problems with that package too.
Try putting jspdf.debug.js
somewhere in your code (I have mine in client/thirdparty/ ), get rid of the var from var jsPDF = (function(global) and give it a shot this way.
Make sure to remove the package and other jspdf js files first!

The package appears to be incompatible with recent Meteor versions (which is weird, as it seems to receive updates). I recommend adding jspdf.js manually to your /client/lib/compatibility folder.
If you don't want to use compatibility folder, you need to ensure the jsPDF object is created in global scope, instead as a local variable in a file. Fortunately, it's easy – just remove var keyword from its declaration. In other words, line 39 should be:
jsPDF = (function () {
instead of
var jsPDF = (function () {

use html-pdf npm package to create dynamic html to pdf file. its very easy to use
Steps:
create html file for report layout design
pass dynamic data in json format by SRS templating methods and get html code.
pass html code in following function
Code here:
var fs = require('fs');
var pdf = require('html-pdf');
var options = { format: 'Letter' };
pdf.create(html, options).toFile('./businesscard.pdf`enter code here`', function(err, res) {
if (err) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});
where html stand for HTML Code.
visit https://www.npmjs.com/package/html-pdf

Related

Reason for node module #kenjiuno/msgreader error: MsgReader is not a constructor

I'm unable to get even the first lines of the example code from the relatively popular #kenjiuno/msgreader for parsing Outlook .msg files to work. I've installed the module with npm successfully, and my code is:
const fs = require('fs')
const MsgReader = require('#kenjiuno/msgreader')
const msgFileBuffer = fs.readFileSync('./test-email.msg')
const testMsg = new MsgReader(msgFileBuffer)
But I get the error: "MsgReader is not a constructor".
A quick console log of MsgReader returns { default: [Function: MsgReader] }. I also tried doing it as a function (no 'new' keyword) which also produced an error.
The only difference between my code and the example code is that they use import (import MsgReader from '#kenjiuno/msgreader') whereas I've used require, but presumably that couldn't make a difference?
Any ideas anyone?
I ended up changing the require statement to add ["default"] which fixed the issue:
const MsgReader = require('#kenjiuno/msgreader')["default"]
I looked at the library code and made a guess based on the export statement using that 'default' syntax. Is this issue something to do with commonJS or something? If anyone can explain this to me that would be great!

How to Use Kuromoji.js in Javascript

I recently installed a package w/ Bower from here: https://github.com/takuyaa/kuromoji.js/
Reading the installation on the github, I basically copied and pasted from the guide:
kuromoji.builder({ dicPath: "../bower_components/kuromoji/dict/" }).build(function (err, tokenizer) {
// tokenizer is ready
var path = tokenizer.tokenize("すもももももももものうち");
console.log(path);
});
However, I do not know what the "kuromoji" should refer to. Here is the obvious error:
Uncaught ReferenceError: kuromoji is not defined
Here is a screenshot of my directory tree:
Not sure how to properly use this.
import kuromoji from 'kuromoji'
That provides the 'kuromoji' object you're trying to reference

export node module to local error

i am a newby on node.js. i want to parse a xml file into json .so i am trying to use bulebutton library from https://github.com/blue-button/bluebutton.js .
first i have installed the module by command npm install bluebutton and its created a node_modules folder with bluebutton module.
now i created a test.js file with following code
var bb = require('bluebutton');
var myRecord = bb.BlueButton('./asd.xml');
console.log(myRecord);
but its gave me an error that bluebutton is not define .please help me to figureout this problem thanks
REVISED ANSWER
From bluebuttonjs.com/docs, the require statement you use would return the BlueButton object, so bb represents said object, and it would be called like so
var myRecord = bb('someFile.xml');
However you might also note that they use fs to read the file before passing it. http://www.bluebuttonjs.com/docs/#parsing-node
PREVIOUS ANSWER (for wrong module)
According to their npm docs, you need to do
var bb = require('blue-button');
https://www.npmjs.com/package/blue-button

AWS Lambda Function is returning "Cannot find module 'index'" yet the handler in the config is set to index

As my title explains I am getting the following error:
{
"errorMessage": "Cannot find module 'index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
I have tried both solutions provided in creating-a-lambda-function-in-aws-from-zip-file and simple-node-js-example-in-aws-lambda
My config currently looks like:
and my file structure is:
and my index.js handler function looks like :
exports.handler = function(event, context) {
What else could be causing this issue aside from what was stated in those two answers above? I have tried both solutions and I have also allocated more memory to the function just incase thats why it couldn't run.
EDIT -
For the sake of trying, I created an even simpler version of my original code and it looked like this:
var Q = require('q');
var AWS = require('aws-sdk');
var validate = require('lambduh-validate');
var Lambda = new AWS.Lambda();
var S3 = new AWS.S3();
theHandler = function (event, context) {
console.log =('nothing');
}
exports.handler = theHandler();
And yet still does not work with the same error?
Try zipping and uploading the contents of the folder lambda-create-timelapse. Not the folder itself.
If this was unclear for anyone else, here are the steps:
Step 1
Navigate to the folder of your project, and open that folder so that you are inside the folder:
Step 2
Select all of the images you want to upload into to Lambda:
Step 3
Right-click and compress the files you have selected:
This will give you a .zip file, which is the file you need to upload to Lambda:
There are a lot of ways to automate this, but this is the manual procedure.
I ran into this problem a few times myself, and this indeed has to do with zipping the folder instead of just the contents like you're supposed to.
For those working from the terminal...
While INSIDE of the directory where the .js files are sitting, run the following:
zip -r ../zipname.zip *
The * is instructing the client to zip all the contents within this folder, ../zipname.zip is telling it to name the file zipname.zip and place it right outside of this current directory.
I had the same problem sometime ago - I reformatted the code.
function lambdafunc1(event, context) {
...
...
...
}
exports.handler = lambdafunc1
The problem occurs when the handler cannot be located in the zip at first level. So anytime you see such error make sure that the file is at the first level in the exploded folder.
To fix this zip the files and not the folder that has the files.
Correct Lambda function declaration can look like this:
var func = function(event, context) {
...
};
exports.handler = func;
You may have other syntax errors that prevent the index.js file from being properly ran. Try running your code locally using another file and using the index.js as your own module.
make sure in your handler following code added
exports.handler = (event, context, callback) => {
...
}
Another reason this can occur is if you don't do an npm install in the folder before packaging and deploying.

browserify error 'Uncaught TypeError: undefined is not a function '

In reference to this question (browserify 'Error: Cannot find module' in trying to fix 'Uncaught reference error: require is not defined'), I got the browserify command to work thanks to stackoverflow community, however, I got another error that tells me
Uncaught TypeError: undefined is not a function bundle.js:10786
Mime.load bundle.js:10786
(anonymous function) bundle.js:10822
_process bundle.js:10848
I believe the function it is complaining about is the 'fs.readFileSync' function. Here is the line 10786 in my 'bundle.js' file
Mime.prototype.load = function(file) {
this._loading = file;
// Read file and split into lines
var map = {},
content = fs.readFileSync(file, 'ascii'), <---LINE 10786
lines = content.split(/[\r\n]+/);
lines.forEach(function(line) {
// Clean up whitespace/comments, and split into fields
var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
map[fields.shift()] = fields;
});
this.define(map);
this._loading = null;
};
In general, you cannot access the file system using Browserify since there's no file system for it to access (remember, you're running this on the web, not just your computer).
However, you can inline the contents of files using the brfs module.

Categories