How I can union two pdf files using suitescript 2 server side - netsuite

I need to union 2 pdf files, on the server site Netsuite script api2.
Or other native javascript option.
Thanks

Use the pdfset element in a template file. This is a fragment from an SS2 Suitelet:
function renderSet(opts){
var tpl = ['<?xml version="1.0"?>','<pdfset>'];
opts.files.forEach(function(id, idx){
const partFile = file.load({id:id});
var pdf_fileURL = xml.escape({xmlText:partFile.url});
tpl.push("<pdf src='" + pdf_fileURL + "'/>");
});
tpl.push("</pdfset>");
log.debug({title:'bound template', details:xml.escape({xmlText:tpl.join('\n')})});
return render.xmlToPdf({
xmlString: tpl.join('\n')
});
}
var pdf = renderSet({files:[file1, file2]});
pdf.name = basename +'_'+ getDateStamp() +'.pdf';
response.writeFile({
file:pdf,
isInline: false
});

Related

Passing a variable between pipes in Gulp 3.9.1

Using gulp 3.9.1
I am attempting to return a bunch of files and perform a task that requires a var to be passed between two pipes.
I'm using node uuid to create a v3 UUID for each file path to
ultimately end up with a uuid for each page. I'm grabbing the file path with gulp-print.
I want to store that uuid value as a var. In the next pipe Im using
gulp-inject-string to write it into the page during the build.
Help: Either I need help getting the file path inside the gulp-inject-string pipe or I need to pass the var between the two different pipes. If I globally set a var with a default value outside the src it gets passed easily to the pipe(inject).
Super simplified code below:
// test code
var gulp = require('gulp');
var print = require('gulp-print');
var inject = require('gulp-inject-string');
var reload = browserSync.reload;
const uuidv3 = require('uuid/v3');
var uuid;
gulp.task('uuid', function() {
return gulp.src('**/*.html'])
// create uuid
.pipe(print(function(filepath) {
uuid = uuidv3(filepath, uuidv3.URL);
return "compiled: " + filepath + ' uuid: ' + uuid;
}))
// need to to add UUIDv3 to each page
.pipe(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">'))
.pipe(gulp.dest('/prod/./'))
.pipe(reload({ stream: true }));
});
It's worth noting that I need a cross platform way to get the file path starting in the root of the project and including forward slashes. The gulp(print) does this perfectly starting at the root of the project and ignoring anything upstream from that point. The format of the path is important because it's one half of the equation in creating the uuid and the uuid's must match on Mac or PC platforms.
examples:
/index.html
/dir1/file.html
/dir1/dir2/dir3/file.html
var gulp = require('gulp');
var print = require('gulp-print');
var inject = require('gulp-inject-string');
const uuidv3 = require('uuid/v3');
var tap = require('gulp-tap');
// you can declare here
var uuid;
gulp.task('pages', function() {
// or you can declare here
var uuid;
return gulp.src('**/*.html')
// bunch of stuff happens here involving templating/minifying
// create uuid
.pipe(print(function(filepath) {
// then set it here and use it further below
// it will be available
uuid = uuidv3(filepath, uuidv3.URL);
return "compiled: " + filepath + ' uuid: ' + uuid;
}))
// need to to add UUIDv3 to each page
//.pipe(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">\n'))
.pipe(tap(function(file, t) {
return t.through(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">\n');
})
.pipe(gulp.dest('/prod/./'))
.pipe(reload({stream:true}));
});
You are just creating a variable at a higher scope that you can set and refer to later. If you need a bunch of them create an array with filepath as an index. But I would try it first as just a simple value.
I solved the problem. It was an amateur mistake. I returned the statement where the var was set so the var was essentially killed. Updated code that allows the var to pass through the pipes.
var gulp = require('gulp');
var print = require('gulp-print');
var replace = require('gulp-replace');
const uuidv3 = require('uuid/v3');
var uuid;
gulp.task('build', function() {
return gulp.src('**/*.html')
// get a cross-platform filepath and create a uuid
.pipe(print(function(filepath) {
uuid = uuidv3(filepath, uuidv3.URL);
}))
// inject uuid
.pipe(replace('dc.identifier" content=""', function() {
return 'dc.identifier" content="' + uuid + '"';
}))
.pipe(gulp.dest('/prod/./'));
});
The var uuid passes through the pipes just fine now. This code creates a UUID based on a cross-platform file path and injects it into an empty dc.identifier meta tag.

Cheerio scraping returning only two rows

So I tested my scraping on a static HTML file before adding it to my Node app.
The problem is that it's not returning all the rows.
On the site:
$('#sport tr').length
//Returns 13
In Cheerio:
$('#sport tr').length
//Returns 2
I'm stumped, here is the code I'm using. I've contained the URL as proof, so you can visit it yourself if you wish.
I'm suspecting it's something to do with var $ = cheerio.load(html); however I'm not experienced in Cheerio to say outright that's the problem.
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', function(req, res){
var url = 'http://www.olbg.com/football.php';
var json = [];
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
console.log($('#sport tr').length);
var headers = [];
$('#sport tr th').each(function(i, th) {
var text = $(th).text();
if (text.trim() !== "") {
headers[i] = text.replace(/[\t\n\r\s]/mgi, '');
}
});
$('#sport tr').each(function(i, tr) {
// skip if header
if (!$(tr).is('th')) {
var temp = {};
temp["Event"] = $(tr).find('td').eq(0).text().trim();
temp["TopSelection"] = $(tr).find('td').eq(1).text().trim();
temp["BookieOdds"] = $(tr).find('td').eq(2).text().trim();
temp["OLBGRating"] = $(tr).find('td').eq(3).find('img').length;
if (temp["Event"] !== "" || temp["TopSelection"] !== ""){
json.push(temp);
}
}
});
}
// To write to the system we will use the built in 'fs' library.
// In this example we will pass 3 parameters to the writeFile function
// Parameter 1 : output.json - this is what the created filename will be called
// Parameter 2 : JSON.stringify(json, null, 4) - the data to write, here we do an extra step by calling JSON.stringify to make our JSON easier to read
// Parameter 3 : callback function - a callback function to let us know the status of our function
fs.writeFile('output.json', JSON.stringify(json), function(err){
console.log('File successfully written!');
})
// Finally, we'll just send out a message to the browser reminding you that this app does not have a UI.
res.send(json);
});
});
app.listen("8081");
console.log("Magic happens on port 8081");
exports = module.exports = app;
The reason that you're not getting the expected result is because the (table) html on that page is mangled. If you look at the second <td> in the second <tr> of the table#sport, you'll see an "extra" </td>. This causes the <td> that the table#sport is inside to close (and an implicit closing of table#sport) on some parsers because that is the closest open <td>. So that is why the parser reports only 2 <tr>s instead of 13. The other <tr>s you're expecting are now outside of table#sport.
Probably your best bet is to pass the html through an HTML tidying program/script (e.g. this one with the clean option enabled) first before passing it to cheerio. After that, your selector should return the elements you're probably expecting.

Get DOM object of input HTML using Node.js

I Have a HTML response. I need to parse it and generate a DOM object. After generation of DOM object I need to search a particular string inside it and get complete hierarchy of HTML tags in which it resides. Is there any NPM package available.
There's an even simpler API for this now in htmlparser2:
var htmlparser = require("htmlparser2");
var dom = htmlparser.parseDOM("<html>your html string</html>");
console.log(dom);
You have htmlparser2 package that can parse HTML stream. You can get the DOM with DomHandler which is bundled with htmlparser2 itself. See the example given there. E.g.
var htmlparser = require("htmlparser2");
var rawHtml = "<html>your html string</html>";
var handler = new htmlparser.DomHandler(function (error, dom) {
console.log(dom);
});
var parser = new htmlparser.Parser(handler);
parser.write(rawHtml);
parser.done();
Parsing DOM object manually is tedious job.
I think that everyone needs a Soup Select(soupselect package) to parse complex DOM objects.
A soupselect is great package in handling DOM.
See following example:
var htmlparser = require("htmlparser2");
var select = require('soupselect').select;
var handler = new htmlparser.DomHandler(function (error, dom) {
if (error)
console.log('error:', error);
else {
// selector reference:
// http://www.w3schools.com/jquery/jquery_ref_selectors.asp
var sel = select(dom, 'body p');
console.log("text in the first <p>: '" + sel[0].children[0].data + "'");
}
});
var parser = new htmlparser.Parser(handler);
var rawHtml =
"<html>"
+ "<head><title>My Title</title></head>"
+ "<body>"
+ "<p>"
+ " Hello World"
+ "</p></body></html>";
parser.parseComplete(rawHtml);
output:
text in the first <p>: ' Hello World'
parseDOM function is now deprecated in htmlparser2 package. You can use parseDocument function now.
const { parseDocument } = require("htmlparser2");
let dom = parseDocument(row_html);
console.log('DOM: ', dom);

Nodejs Backbone Templates

I have worked a lot with rails, requirejs and backbone and know how to use haml coffee templates in rails.
App = new Backbone.Marionette.Application()
App.addInitializer (options) ->
Backbone.history.start()
alert "yay"
$ ->
alert "yay"
App.start()
How do i do it in Node.js, I have a Node.js app and i am at a deadend with regards to how do i get a template to compile client side, i am not stuck on haml coffee, any template engine will do, jade is fine too, underscore too. Just a good starting point so that i can get on with building the backbone app in node.js.
Any Help is appreciated!
I don't suggest dragging the templates to the client and compiling them there,the right way would be to use some framework such as www.socketstream.com that offers what you want and much more. If you're against frameworks quick and dirty solution to compiling them on the server and calling them as function on the client will be :
// compile.js
var fs = require("fs")
,jade = require("jade");
exports.build = function(templatesDir) {
var js = "var Templates = {}; \n\n";
var files = fs.readdirSync(templatesDir);
var jadeFiles = files.filter(function(file) {
return file.substr(-5) === ".jade";
});
for(var i = 0; i < jadeFiles.length; ++i){
var filePath, key;
var file = jadeFiles[i];
key = file.substr(0, file.indexOf("."));
filePath = templatesDir + file;
var jadeSource = fs.readFileSync(filePath);
js += "Templates." + key + " = " + jade.compile(jadeSource, {
debug: false,
client: true
}).toString() + "; \n\n";
}
return js;
};
// On the server.js
// Compile views
var viewsPath = path.join(__dirname, 'views/');
var generatedJs = require('./compile').build(viewsPath);
var savePath = path.join(__dirname, 'public/js/lib/templates.js');
fs.writeFile(savePath, generatedJs, function (err) {
if (err) throw err;
});
// Then on the client include js/lib/templates.js and use templates like this
FactSummaryView = Backbone.View.extend({
template: Templates.issueSummary,
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
// Also add templates.js to nodemonignore if you're using nodemon
./public/js/lib/templates.js
/public/js/lib/templates.js
You usually don't compile the templates on the client (expect the templates are edited directly in the browser), instead they are compiled in the backend and rendered in the browser.
Compile the templates
In this step you compile the template source code to a JavaScript file that contains a render function.
You can either use haml-coffee on the command line and make a script in your build process or make use of the projects listed in the integration section of the Haml-Coffee README.
Grunt is a popular solution to run certain tasks and with Grunt-Haml you have certainly a flexible build solution for your project.
Render the templates
To render the templates with Marionette you need to make sure the template render function is available on the client. Just type the configured namespace into the developer tools to see if the template functions are registered:
If this is fine, you need to have a custom template render function:
Backbone.Marionette.Renderer.render = (template, data) ->
if JST[template]
JST[template](data)
else if _.isFunction(template)
template(data)
else
console.error 'Template %s not found', template
Now you can simply define the view template and it'll be rendered properly:
class App.Views.Login extends Backbone.Marionette.ItemView
template: 'shared/_login'

How to read image from Application folder in winjs

How to read image from Application folder in winjs
var item = groupedProducts.getAt(indx);
item.img = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + "3766111.jpg";
groupedProducts.setAt(indx, item);
WinJS.UI.processAll();
You need to use the async APIs to access files in ApplicationData in WinJS, such as the getFileAsync function used below (this is a helper function I use in databinding for one of my apps):
function getLocalLargeMapTile(item) {
return new WinJS.Promise(
function (completed, error, progress) {
var filename;
var sourceFolder;
if (item.latlong) {
var latandlong = item.latlong.split(", ");
var lat = latandlong[0];
var lon = latandlong[1];
filename = lat + lon + ".png";
var appData = Windows.Storage.ApplicationData.current;
sourceFolder = appData.localFolder;
sourceFolder.getFileAsync(filename).then(function (file) {
var mapUrl = window.URL.createObjectURL(file, { oneTimeOnly: true });
completed(mapUrl);
},
function (error) {
handleError(error)
});
}
else {
filename = "ms-appx:///images/megaphone_256x256.png";
completed(filename);
}
}
);
}
What I'm doing in the helper function is checking whether my data includes a latitude and longitude, and if so, checking for a file with a matching filename, and since those files are in the Application Data folder, wrapping the file with an objectURL and returning a promise with the objectURL. Otherwise, I simply return an ms-appx url pointing to a static file in the app's images folder. Here's how I call this helper function, from a programmatic template (I don't think you can do this with a declarative template):
var image = document.createElement("img");
image.className = "item-image";
image.src = "ms-appx:///images/megaphone_256x256.png";
result.appendChild(image);
// additional code omitted
var promise = mapTileUtil.getLocalMapTile(currentItem);
promise.done(function (mapTileUrl) {
image.src = mapTileUrl;
});
For more info on templating functions, which provide greater control over the rendered markup than declarative templates, check out:
http://msdn.microsoft.com/en-us/library/windows/apps/jj585523.aspx
and
http://go.microsoft.com/fwlink/p/?linkid=231499
For more information on Windows Store app development in general, register for App Builder.

Resources