foliojs / pdfkit Goto Github PK
View Code? Open in Web Editor NEWA JavaScript PDF generation library for Node and the browser
Home Page: http://pdfkit.org/
License: MIT License
A JavaScript PDF generation library for Node and the browser
Home Page: http://pdfkit.org/
License: MIT License
I have an idea for it. We can pass a two-dimensional array of texts, to a method with presentation options like the following:
doc.table([
["cell11"],["cell21"],["cell31"],
["cell12"],["cell22"],["cell32"],
["cell13"],["cell23"],["cell33"]
],{
width:20,
height:40,
x:30,
y:40
});
Then in the module we make rectangles and texts for each cell.
I'm finding that the doc.image function draws some png files with corrupted blocks of odd colour..
When using the image function to draw an image, if you specify 0 for x or y, either as parameters or in the options object, the method will fall back to using @x and @y, because the number 0 equates to false in Javascript truth.
Specifically, the lines I'm referring to are found at https://github.com/devongovett/pdfkit/blob/master/lib/mixins/images.coffee#L13 and https://github.com/devongovett/pdfkit/blob/master/lib/mixins/images.coffee#L14 at the time of this writing.
I'm not too fluent with coffeescript, so I don't know if there is an easy way to resolve this without resorting to checking if the variables are undefined.
/Users/rcameron/node_modules/pdfkit/lib/mixins/line_wrapper.coffee:63
len = words.length;
^
TypeError: Cannot read property 'length' of null
at LineWrapper.wrap (/Users/rcameron/node_modules/pdfkit/lib/mixins/line_wrapper.coffee:63:20)
at PDFDocument.text (/Users/rcameron/node_modules/pdfkit/lib/mixins/text.coffee:46:17)
at /Users/rcameron/pdfMaker/node-test.js:90:8
at IncomingMessage. (/Users/rcameron/pdfMaker/node-test.js:61:4)
at IncomingMessage.emit (events.js:81:20)
at HTTPParser.onMessageComplete (http.js:133:23)
at CleartextStream.ondata (http.js:1231:22)
at CleartextStream._push (tls.js:303:27)
at SecurePair.cycle (tls.js:577:20)
at EncryptedStream.write (tls.js:96:13)
I am seeing this issue when adding text via the doc.text method. I changed the pdfkit module to test if words was null before testing length on line 54 of line_wrapper.coffee.
if !!text.match(WORD_RE)
words = text.match(WORD_RE)
This solved the issue.
I working on a document with some lists of which the text is wider than the page. It would be nice if we could supply a width to the list for wrapping.
Is there anyway to add a footer section to all pages? because i want to add pageNumbers(ex: page 1 of 5) to all pages... and because my content is dynamic i can't hardcode the number total of pages because i cant predict how many pages the document will have...
Thanks in advance.
To keep your interfaces consistent I feel x and y should default to the current x and y.
As I stated on twitter you can use doc.list(["",""], doc.x, doc.y) as a temporary solution.
using this
doc.registerFont('Helvetica Neue', '/System/Library/Fonts/HelveticaNeue.dfont', 'Helvetica Neue')
leads to the following error:
TypeError: Cannot read property 'named' of undefined
at DFont.getNamedFont (/Users/adrien/Playground/CV/node_modules/pdfkit/lib/font/dfont.coffee:71:28)
at Function.fromDFont (/Users/adrien/Playground/CV/node_modules/pdfkit/lib/font/ttf.coffee:26:32)
at new PDFFont (/Users/adrien/Playground/CV/node_modules/pdfkit/lib/font.coffee:32:28)
at PDFDocument.font (/Users/adrien/Playground/CV/node_modules/pdfkit/lib/mixins/fonts.coffee:33:20)
at Object.<anonymous> (/Users/adrien/Playground/CV/cv.coffee:8:7)
at Object.<anonymous> (/Users/adrien/Playground/CV/cv.coffee:9:4)
at Module._compile (module.js:402:26)
at Object.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script.js:57:25)
at /usr/local/lib/node_modules/coffee-script/lib/command.js:147:29
at /usr/local/lib/node_modules/coffee-script/lib/command.js:115:26
When trying to write PDF document I get following error:
TypeError: Object # has no method 'deflate' at
PDFReference.finalize (/usr/local/lib/node/.npm/pdfkit/0.1.5/package/lib/reference.coffee:54:33)
Here's the line in reference.coffee:
data = new Buffer(data.charCodeAt(i) for i in [0...data.length])
compressedData = zlib.deflate(data)
I've checked object zlib and it has the method 'deflate' but it named 'Deflate' now (with capitalised first letter).
Versions: [email protected], [email protected]
Or indent paragraphs... Hard to read otherwise.
Enhancement request:
It would be great if pdfkit could support an arc function like HTML5 Canvas.
From the canvas spec:
void arc(double x, double y, double radius, double startAngle, double endAngle, optional boolean anticlockwise);
Currently it doesn't appear possible to draw arcs using pdfkit....
Thanks!
I found the array of supported sizes in page.coffee. It would be nice if those pixel dimensions were posted in the documentation for quick reference.
Here is a temporary fix I am currently using.
list: (array, @x, @y) ->
gap = Math.round (@_font.ascender / 1000 * @_fontSize) / 2
for item in array
@circle x + 3, @y + gap + 3, 3
@text item, x + 15
@y += 3
@x -= 15
@fill()
I'm trying to put some real transformation from a svg file created by illustrator, but the transform method doesn't work as it should be.
the file (test.js) has one line:
var PDFDocument = require ('pdfkit');
and it throws this error:
/mydir/node_modules/pdfkit/lib/document.coffee:1
^
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:402:25)
at Object..js (module.js:413:10)
at Module.load (module.js:339:31)
at Function._load (module.js:298:12)
at require (module.js:351:19)
at Object. (/mydir/test.js:1:81)
at Module._compile (module.js:407:26)
at Object..js (module.js:413:10)
at Module.load (module.js:339:31)
at Function._load (module.js:298:12)
It might be really convenient to have a roundRect function - like the ReportLabs pdf canvas has.
Of course if is possible to do find a workaround using lines and bezier curves at the corners. But a simpler helper function would be great..
I'd love to use pdfkit to read an existing PDF in, get a list of form fields (text fields), populate them with data and write a new document out.
Thanks.
I found that (accidentally!) setting lineWidth = -1 on PDFDocument before drawing shapes results in Adobe Reader not loading the PDF file.
Might be worth a gate to prevent other people accidentally doing the same thing ;-)
All the returns in PDFobject::convert could be removed for clarity, and, in fact, one branch (Dates) doesn't even use one.
PDFKit is a great tool that we want to see it even better.
Couple of things that might be worth looking into:
I am not sure if these are already in but I would like to mentioned them anyway:
Example for this:
var coffee = require('coffee-script'),
pdfkit = require('pdfkit'),
pdfdoc = new pdfkit;
pdfdoc.rotate(90, {origin:[20,20]})
pdfdoc.text('hi',20,20)
pdfdoc.write('test.pdf');
But if instead of 90 we use 90.0001, it works(90.00001 or smaller doesn't work). Same thing goes for 270 degrees.
PDFDocument.text appears to throw an exception when passed a number to draw, instead of a string, e.g., document.text(23, 10, 10);
It might be nicer to call toString() on the first argument?
When I set the align
option to justify
on an embedded TTF font, the text will still be left aligned. I have tried with three different fonts, but this one will probably be good enough for testing.
Sorry I haven't investigated the problem further before posting this issue.
Is there any way to do this? For example we have made 3 pages, we write something on the 3rd page, and then write something else on the 2nd.
Perhaps a simple HTML parser as well?
when i try to do this
doc.text('?') it gives the following error:
/Users/NeCkEr/.node_libraries/.npm/pdfkit/0.1.5/package/lib/mixins/text.coffee:178
wrap.extraSpace = (options.wordSpacing || 0) * (words.length - 1) + (opt
^
TypeError: Cannot read property 'length' of null
Can you provide some documentation that how does it work for developers? Actually on the part converting javascript objects to pdf. I found it confusing.
Thanks
in my case, Korean lanuage.
I write simple test for converting text.
English is ok. but Korean is not...
if you need fonts, I will send Korean fonts to you.
Opening the example PDF document at http://devongovett.github.com/pdfkit/example.pdf in Adobe Reader 10.1.0 and scrolling down to the fourth page, Adobe Reader displays the error message "Cannot extract the embedded font 'Chalkboard'. Some characters may not display or print correctly".
I uploaded a couple of screenshots to: http://imgur.com/a/7ZomJ
Is there support for utf8?
I will try do:
doc.text('Бла бла бла', 0, 0);
doc.write('test.pdf');
in the resulting document, I see it => ;0 1;0 1;0
Difficult problem to understand and to explain.
When I write the text "une forêt" the pdf has "une fforêt" (2 f).
After hours looking for the problem, I discovered that my f was encoded "" on class Subset. String.fromCharCode(@unicodes['f']) === ""
On mixin "text" function _escape replace \ by \ ... I don't know /understand why but removing this replace fix my problem and I have no time to investigate deeper..
down in loca.coffee and other files there's
Data = require '../../Data'
but this doesn't seem to be the right path... shouldn't it be ../data ? causes a failure on requiring pdfkit itself...
Also, page margins.
node v0.6.1
pdfkit v0.1.6
Code:
var pdf = require('pdfkit');
Error log:
Error: Cannot find module './zlib_bindings'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (/home/user/share/test_pdfkit/node_modules/pdfkit/node_modules/flate/lib/zlib.js:1:80)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
Hi!
Currently I am using PDFKit and i have a good results.
So, I'm trying to find something about how to change the page orientation of the document. Can you help me? some tip ?
I `m working with ruby on rails
thanks for attention
Fillipe Norton
Is there a way to have text in a left column (pane) that controls what is visible on the right? I'd like to be able to click on something on the left and have the visibility of images on the right change. Even more specifically, I'd like to have images overlayed and control which overlays are present. The overlays would ideally be any pdf content, but could be limited to PNGs.
Can these features be implemented in pdfkit, and if not, are they possible at all in the PDF format?
Also, does every PDF reader, such as those on the mobile devices iPhone and Android support such interactivity?
I'm sorry to ask so many questions, but I've googled PDF for hours and I can't really figure out what is possible.
When I use cmyk color values ( e.g. .strokeColor([0,0,0,100]), the PDF is created successfully, but is not readable by any PDF viewer, even if I only use CMYK colors. CMYK colors were introduced by this commit: 6a9d2f8
I think there has to be a document-wide property that defines the correct color space (something like DeviceCMYK).
The document.write function should allow a callback function to be passed through to fs.writeFile.
Otherwise one doesn't know when the file has actually been written..
Nice library!
As the current version (0.1.5), it is able to put asian (zh-tw in my case) language characters in a text, with proper (not default) font file supplied. That is really good!
Unfortunately the texts cannot be correctly wrapped. So the paragraph will display in a single line and go right all the way outside the page boundary. If we five paragraphs, for example, then there will be five lines of text. No matter how many characters each paragraph have.
I think it is a little bit difficult to very correctly wrap Chinese texts. But I guess there might be some work around not too difficult.
In Javascript, not coffee:
PDFDocument = require('pdfkit');
doc = new PDFDocument();
// register and use a font, set the font size, and render some text
doc.registerFont('hn', __dirname + '/public/fonts/HelveticaNeue.ttf');
doc.registerFont('hnl', __dirname + '/public/fonts/HelveticaNeueLt.ttf');
doc.font('hn')
.fontSize(25)
.text('Some text with an embedded font!', 100, 100);
// Add another page
doc.addPage()
.fontSize(25)
.text('Here is some vector graphics...', 100, 100);
// Write the PDF file to disk
doc.write('output.pdf');
Where's the problem?
When using the window side by side with another the text it truncated because the website doesn't allow wrapping when the main can't be 800px. Just commenting that out makes the page more readable. 1/2 of a HD 1920 px screen is 960 and with your 250px sidebar I need at least 1050 to read the page.
There is a bug where pdfkit doubles a character.
For example, if I write:
doc.text("Tel (787) 760-3000");
I get different results:
Tel (787))760-3000
or
Tel (787) 760--3000
or
Tel (787) 760-300000
I haven't been able to pinpoint how to fix this. But I found that if I write a dash before the one in between the numbers:
doc.text("Tel - (787) 760-3000");
It doesn't double the dashes, it doubles the parenthesis.
Very confusing. Thanks!
From line 33 of Reference.coffee:
data = @stream.join '\n'
if @stream
...
This throws an exception if stream is null. Swapping the lines fixes the bug:
if @stream
data = @stream.join '\n'
Here is the error message that I get when I use Passenger to run my Ruby on Rails app and try to create a PDF page. I am using PDFKit Middleware.
wkhtmltopdf runs fine on the command line, and it also runs fine when I launch my Rails app using script/server.
/usr/local/bin/wkhtmltopdf: error while loading shared libraries: libQtSvg.so.4: cannot open shared object file: No such file or directory
/!\ FAILSAFE /!\ Sun Aug 07 13:30:38 -0500 2011
Status: 500 Internal Server Error
Broken pipe
/usr/lib/ruby/gems/1.8/gems/pdfkit-0.5.2/lib/pdfkit/pdfkit.rb:65:in write' /usr/lib/ruby/gems/1.8/gems/pdfkit-0.5.2/lib/pdfkit/pdfkit.rb:65:in
puts'
/usr/lib/ruby/gems/1.8/gems/pdfkit-0.5.2/lib/pdfkit/pdfkit.rb:65:in to_pdf' /usr/lib/ruby/gems/1.8/gems/pdfkit-0.5.2/lib/pdfkit/pdfkit.rb:64:in
popen'
/usr/lib/ruby/gems/1.8/gems/pdfkit-0.5.2/lib/pdfkit/pdfkit.rb:64:in to_pdf' /usr/lib/ruby/gems/1.8/gems/pdfkit-0.5.2/lib/pdfkit/middleware.rb:21:in
call'
Please help - PDFKit is amazing - thanks for your work on it!
Don
Found a bug, don't know if the future update will fix it, but if you have an abbreviation, e.g. R.E.B.E.L.S., and it's on the of a line it separates the abbreviation instead of leaving it all together, e.g. R.E.B. \n E.L. .
Did the example make sense?
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.