Maybe too specific of a question, but...the task is to read an image from a https url and insert it into a pdf. What I have working in code not shown is writing text and drawing stuff to pdf as well as inserting images from the filesystem.
Using node, https, pdfkit
Code:
const PDFDocument = require('pdfkit');
const https = require('https');
let pdf = new PDFDocument;
let writeSecondStream = fs.createWriteStream('out.pdf');
pdf.pipe(writeSecondStream);
let body = [];
https.get('https://compote.slate.com/images/c9320bfa-e49d-41af-bccc-f85351038055.jpg', function(res) {
if (res.statusCode != 200) {
return console.log('HTTP Response code ' + res.statusCode);
}
res.on('data', (chunk) => {
console.log(chunk);
body.push(chunk);
}).on('end', () => {
let allbody = body.join();
console.log("allbody = ", allbody);
pdf.image(allbody, 25, 25);
})
});
pdf.end();
with console.log(allbody) commented out
console.log(chunk) is like 20 lines of this:
Code:
<Buffer 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a2 8a 28 00 a4 a5 a4 eb 40 05 14 ... >
<Buffer c7 91 f4 35 d1 1c 5c 5b b3 56 29 49 33 a3 a2 a2 b7 b9 8a e6 3d f1 36 40 ea 31 c8 fc 2a 5a ea 4d 35 74 50 51 da 8a 29 80 52 52 d1 40 09 4b 40 f6 a2 80 ... >
console.log(allbody) makes everything in the console look like:
Code:
��o}�،��<z��V��ư�G�#����(:U��B��UsK�sK��n��x��
�um� =��� ��́�?�I��v�������.�������H/m[��k�.�cpO@V��{!�Tz���]������:�n�*���4��y2�L�i*�2:U'.�^E������G�`�`P�ґ��}�����S�ڋ˰��Mw� ��T��j���q�
if console.log(allbody) is commented out the output ends with this error
Code:
fs.js:162
throw er;
^
Error: Path must be a string without null bytes
at nullCheck (fs.js:159:14)
at Object.fs.openSync (fs.js:645:3)
at Object.fs.readFileSync (fs.js:551:33)
at Function.PDFImage.open (C:\Users\jayne\nodeauthapi\node_modules\pdfkit\js\image.js:32:21)
at PDFDocument.openImage (C:\Users\jayne\nodeauthapi\node_modules\pdfkit\js\mixins\images.js:102:26)
at PDFDocument.image (C:\Users\jayne\nodeauthapi\node_modules\pdfkit\js\mixins\images.js:30:24)
at IncomingMessage.res.on.on (C:\Users\jayne\nodeauthapi\app.js:499:9)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
[nodemon] app crashed - waiting for file changes before starting...
I figure it's some kind of encoding/decoding/headers thing, but have been trying various things for hours. ?
Last edited by microbet; 11-01-2018 at 10:50 PM.