0

I am trying to generate a pdf using some texts of my own (and not a html page). I tried using PDFKit. I am able to generate the pdf but then I am not able to open it in Preview (It may be damaged or use a file format that Preview doesn’t recognize.)

Below is the code,

var fs = require('fs');
PDFDocument = require ('pdfkit');
var doc = new PDFDocument

// Embed a font, set the font size, and render some text
doc.text('Some text with an embedded font!', 100, 100)

// Add another page
doc.addPage()
   .text('Here is some vector graphics...', 100, 100)

// Draw a triangle
doc.save()
   .moveTo(100, 150)
   .lineTo(100, 250)
   .lineTo(200, 250)
   .fill("#FF3300")

// Apply some transforms and render an SVG path with the 'even-odd' fill rule
doc.scale(0.6)
   .translate(470, -380)
   .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
   .fill('red', 'even-odd')
   .restore()

// Add some text with annotations
doc.addPage()
   .fillColor("blue")
   .text('Here is a link!', 100, 100)
   //.underline(100, 100, 160, 27, color: "#0000FF")
   .link(100, 100, 160, 27, 'http://google.com/')

// Write the PDF file to disk
doc.pipe(fs.createWriteStream('out.pdf') );
doc.end

I might be missing something small. P

Abhishek Kumar
  • 183
  • 2
  • 9
  • Can you post the PDF generated by your sample above? If it's not being recognized (as a PDF document), it may be missing a PDF identifier within the first... um, 128 bytes, is it?... of the start of the file. – acg_so Apr 28 '16 at 20:37
  • Got it. I should have used doc.end() instead of doc.end. It's working now. Thanks. – Abhishek Kumar Apr 29 '16 at 16:24

1 Answers1

2

It was a silly mistake on my part. I should have used doc.end() instead of using doc.end and due to this the pdf was not getting generated properly.

Abhishek Kumar
  • 183
  • 2
  • 9
  • 1
    I've got same mistake and fixed it, but i don't get it how it works similary without any warning or errors -_- haha – kakadais Oct 11 '17 at 04:10