How to use HummusJS operators to adjust text space? - node.js

HummusJS is a great lib make pdf for node js.
but I can't understand how to use operators like TJ.
How to use text, hex or code on it?
I have try it as like as example
var newPdf = new hummus.PDFWStreamForFile('temp.pdf');
var pdfWriter = hummus.createWriter(newPdf);
var page = pdfWriter.createPage(0, 0, 595, 842);
var pageContent = pdfWriter.startPageContentContext(page);
pageContent.BT()
.k(0,0,0,1)
.TJ('H',3,'ES')
.Tm(30,0,0,30,78.4252,662.8997)
.ET();
but It doesn't work.
thanks help

Related

Can't set the value of textItem.contents

I am trying to write a script that will update the value of a text layer in Photoshop.
I have a layer stored in a variable myLayer which I log out so I know it is an ArtLayer with a type of LayerKind.TEXT and has a textItem object associated that has a string value in place. All as I would expect.
The documentation says that textItem.contents is read-write so I thought myLayer.textItem.contents = "Hello World" should update the value but when I try this I get General Photoshop Error occurred. This functionality might not be available in this version of Photoshop.
Can anyone advise on what I'm missing?
I am using Photoshop CC 2014 and the CC 2014 Javascript Reference
Thankyou in advance for you help :)
Its hard to see whats going wrong when there is no code example. This works for me.
Tested in PS CC 2014 Mac OSX
// needs a Photoshop document with only one textlayer
var d = app.activeDocument;
var l = d.artLayers[0];
if(l.hasOwnProperty ("textItem")){
$.writeln("yes");
l.textItem.contents = "Hello World";
}
This works in CS2: (Assuming the active layer (mylayer) is a text layer)
var srcDoc = app.activeDocument;
var myLayer= srcDoc.activeLayer;
var text = myLayer.textItem.contents;
myLayer.textItem.contents = "Hello World";

PDFKit, nodeJS merging two PDF files

does anyone have experience with PDFKit with NodeJS. Specifically, I'm trying to merge 2 PDF documents into 1, but I can't seem seem to get the content of the two PDFs properly with formatting inside the merged one.
Here's what I do:
var PDFDocument = require('pdfkit');
var fs = require('fs');
var doc = new PDFDocument();
var fileName = 'test.pdf';
doc.pipe(fs.createWriteStream(fileName));
var file1 = '1.pdf';
var file2 = '2.pdf';
var stream1 = fs.createReadStream(file1);
doc.text(stream1);
doc.addPage();
var stream2 = fs.createReadStream(file2);
doc.text(stream2);
doc.end();
The output, that being test.pdf, should consist of a single pdf containing the contents of the 2 pdfs with the same formatting, but I'm only getting test.pdf with 2 pages, each consisting of a single line of "[Object object]". I can't seem to find how to redirect the content of the stream inside the doc.text() function.
Any idea on what I do wrong, how should I fix it?
It is not possible to merge two PDF documents with pdfkit!
You can use pdftk Server for that purpose. The program offers a command line interface, which could merge two pdfs with the following command:
pdftk 1.pdf 2.pdf cat output merged.pdf

greasemonkey simple string manipulation

I need a little help to write a simple greasemonkey script to help me with a task.
I have a an url with a number like this:
www.something.com/item/4563/
and I want to extract this number from the url, convert it to integer variable (called item_number) so I can use it in this simple code to generate link to pages with previous and next items:
var links = document.createElement("div");
links.innerHTML = '<a href="http://www.something.com/item/' + (item_number-1) +
'/">Previous</a> <a href="http://www.something.com/item/' + (item_number+1) +
'/">Next</a>';
document.body.insertBefore(links, document.body.firstChild);
Thanks in advance for any help. Cheers!
var url = "www.something.com/item/4563/";
var matches = url.match(/item\/(\d+)/);
var itemNumber = matches && parseInt(matches[1], 10);
console.log(itemNumber); // => 4563

Populate iTextSharp pdf with database values

I have this code below and trying to populate my pdf with values from the database.
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to ", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
// code below needs attention
var cID = "ALFKI";
var xw = Customers.First(p => p.CustomerID ==cID);
table.AddCell(xw.CompanyName.ToString());
I can not figure out where I am going wrong. When I remove the code under 'code below needs attention' it works but I need the database values.
I am using webmatrix with ItextSharp. If to answer you need further code please let me know.
PdfPCell cell=new PdfPCell(new Phrase(xw.CompanyName.ToString()));
cell.setColspan(numColumns);
table.addCell(cell);
document.add(table);

Insert image into a specified location

I have a Google Apps script which replaces placeholders in a copy of a template document with some text by calling body.replaceText('TextA', 'TextB');.
Now I want to extend it to contain images. Does anybody have idea how to do this?
Thank you,
Andrey
EDIT: Just to make it clear what my script does. I have a Google form created in a spreadsheet. I've created a script which runs upon form submission, traverses a sheet corresponding to the form, find unprocessed rows, takes values from corresponding cells and put them into a copy of a Google document.
Some fields in the Google form are multi-line text fields, that's where '\r\r' comes from.
Here's a workaround I've come up with by now, not elegant, but it works so far:
// replace <IMG src="URL"> with the image fetched from URL
function processIMG_(Doc) {
var totalElements = Doc.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = Doc.getChild(j);
var type = element.getType();
if (type =='PARAGRAPH'){
var par_text = element.getText();
var start = par_text.search(new RegExp('<IMG'));
var end = par_text.search(new RegExp('>'));
if (start==-1)
continue;
// Retrieve an image from the web.
var url = getURL_(par_text.substring(start,end));
if(url==null)
continue;
// Before image
var substr = par_text.substring(0,start);
var new_par = Doc.insertParagraph(++j, substr);
// Insert image
var resp = UrlFetchApp.fetch(url);
new_par.appendInlineImage(resp.getBlob());
// After image
var substr = par_text.substring(end+1);
Doc.insertParagraph(++j, substr);
element.removeFromParent();
j -= 2; // one - for latter increment; another one - for increment in for-loop
totalElements = Doc.getNumChildren();
}
}
}
Here is a piece of code that does (roughly) what you want.
(there are probably other ways to do that and it surely needs some enhancements but the general idea is there)
I have chosen to use '###" in the doc to mark the place where the image will be inserted, the image must be in your google drive (or more accurately in 'some' google drive ).
The code below uses a document I shared and an image I shared too so you can try it.
here is the link to the doc, don't forget to remove the image and to put a ### somewhere before testing (if ever someone has run the code before you ;-)
function analyze() { // just a name, I used it to analyse docs
var Doc = DocumentApp.openById('1INkRIviwdjMC-PVT9io5LpiiLW8VwwIfgbq2E4xvKEo');
var image = DocsList.getFileById('0B3qSFd3iikE3cF8tSTI4bWxFMGM')
var totalElements = Doc.getNumChildren();
var el=[]
for( var j = 0; j < totalElements; ++j ) {
var element = Doc.getChild(j);
var type = element.getType();
Logger.log(j+" : "+type);// to see doc's content
if (type =='PARAGRAPH'){
el[j]=element.getText()
if(el[j]=='###'){element.removeFromParent();// remove the ###
Doc.insertImage(j, image);// 'image' is the image file as blob
}
}
}
}
EDIT : for this script to work the ### string MUST be alone in its paragraph, no other character before nor after... remember that each time one forces a new line with ENTER the Document creates a new paragraph.

Resources