I am able to generate a pdf using html-pdf in nodejs locally on my mac. The minute I deploy the my GET api using serverless on AWS everything fails. The pdf does not get generated and I get 400 bad request and message as
I changed version of the html-pdf package , tried installing fontmanager but nothing helped.
//pdf generation class
{
.....
const pdfSettings = {
"border": '1cm',
"header": {
"height": "15mm"
}
};
const PDFBuffer = new Promise((resolve, reject) => {
pdf.create(pdfContent, pdfSettings).toBuffer(
(err, buffer) => {
err ? reject(err) : resolve(buffer);
});
});
const buffer = await PDFBuffer;
return actions.downloadPDF(contactId, buffer);
}
//function which returns pdf in binary to api gateway
downloadPDF(pdfName, pdfBuffer) {
let responseObj = {
statusCode: 200,
isBase64Encoded: true,
headers: {
'Content-type': 'application/pdf',
'accept-ranges': 'bytes',
'Content-Disposition': 'attachment; filename=' + pdfName + '.pdf'
},
body: pdfBuffer && JSON.stringify(pdfBuffer.toString('base64'))
}
return responseObj;
}
I should be able to generate pdf using the GET api in AWS. Please let me know any valuable suggestion that could help me with this issue. Thanks in advance
faced the same issue with html-pdf on aws ubuntu and here is what I did.
test html-pdf package using the example:
root@APP-AP-1A-01:/home/user01/server-a7# node node_modules/html-pdf/bin/index.js node_modules/html-pdf/examples/businesscard/businesscard.html /tmp/test-prod.pdf
/home/user01/server-a7/node_modules/html-pdf/bin/index.js:30
if (err) throw err
^
Error: html-pdf: Received the exit code '127'
/home/user01/server-a7/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
at ChildProcess.respond (/home/user01/server-a7/node_modules/html-pdf/lib/pdf.js:121:31)
at ChildProcess.emit (events.js:210:5)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
^ this error was seen on Aws running Ubuntu (4.4.0-1072-aws) and it was due to missing library phantomjs is dependent on; the npm -i
did not install/ update it quite well on the server.
Here are the steps:
run as root user or use sudo :-
root@APP-AP-1A-01:/home/user01/server-a7# ldd /home/user01/server-a7/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
linux-vdso.so.1 => (0x00007ffea155d000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fd639645000)
libfontconfig.so.1 => not found
libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007fd63939b000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd639197000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd638f8f000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd638d72000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd6389f0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd6386e7000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd6384d1000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd638107000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd63985f000)
libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007fd637ee2000)
root@APP-AP-1A-01:/home/user01/server-a7# ls -l /usr/lib/x86_64-linux-gnu/libfontconfig.so.1
ls: cannot access '/usr/lib/x86_64-linux-gnu/libfontconfig.so.1': No such file or directory
root@APP-AP-1A-01:/home/user01/server-a7# apt-get install fontconfig
Reading package lists... Done
:
The following additional packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1
The following NEW packages will be installed:
fontconfig fontconfig-config fonts-dejavu-core libfontconfig1
:
Do you want to continue? [Y/n] y
Get:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-dejavu-core all 2.35-1 [1,039 kB]
Get:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 fontconfig-config all 2.11.94-0ubuntu1.1 [49.9 kB]
Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libfontconfig1 amd64 2.11.94-0ubuntu1.1 [131 kB]
Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 fontconfig amd64 2.11.94-0ubuntu1.1 [178 kB]
Fetched 1,398 kB in 1s (928 kB/s)
:
:
Regenerating fonts cache... done.
Processing triggers for libc-bin (2.23-0ubuntu11.2) ...
root@APP-AP-1A-01:/home/user01/server-a7# ldd /home/user01/server-a7/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
linux-vdso.so.1 => (0x00007ffcdf49c000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f65a4723000)
libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f65a44e0000)
libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f65a4236000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f65a4032000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f65a3e2a000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f65a3c0d000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f65a388b000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f65a3582000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f65a336c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f65a2fa2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f65a493d000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f65a2d79000)
libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007f65a2b54000)
root@APP-AP-1A-01:/home/user01/server-a7# node node_modules/html-pdf/bin/index.js node_modules/html-pdf/examples/businesscard/businesscard.html /tmp/test-prod.pdf
root@APP-AP-1A-01:/home/user01/server-a7# ls -lrt /tmp/test-prod.pdf
-rw-r--r-- 1 root root 11250 Jul 10 10:05 /tmp/test-prod.pdf
created the pdf in /tmp/test-prod.pdf
I was able to reach a better solution by using puppeteer library. Reasons for choosing a different library
Although to make this work in AWS with serverless & serverless-plugin-optimize , I did face many challenges. Note out the following points while implementing this kind of similar scenario
*/*
in API Gateway resource Settings options , if going through serverless under provider in serverless.yaml add provider 下通过添加
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.