I need to create a pdf file from an HTML on the server-side (dotnet core 2) and send it as an attachment of an email. I have created a node service (createPdf.js) as follows and kept it in a local directory (NodeService) within the solution -
module.exports = function (callback, html, outputFilePath) {
var pdf = require('html-pdf');
var options = { format: 'A3', orientation: 'portrait' };
pdf.create(html, options)
.toFile(outputFilePath, function (err, res) {
if (err)
return callback(null, false);
callback(null, true);
});
}
And I am triggering this function as follows -
public static async Task<bool> GeneratePdf(INodeServices nodeService, string html, string outputFilePath)
{
string pdfGenerationNodeService = Path.Combine(Directory.GetCurrentDirectory(), "NodeService", "createPdf.js");
try
{
return await nodeService.InvokeAsync<bool>(pdfGenerationNodeService, html, outputFilePath);
}
catch (Exception ex)
{
throw;
}
}
For calling this method from the controller -
public async Task<IActionResult> SendQuotationToHospitalAsync([FromServices]INodeServices nodeService, int id)
{
...
bool isAdminPdfGenerationSuccess = await PdfHelperService.GeneratePdf(nodeService, htmlContent, filePath);
...
}
I have also registered the node service in StartUp.cs -
services.AddNodeServices();
When I am triggering the function in debug mode, it's working properly and the pdf file is getting generated. However, once I deploy the application on the server, the node function is not getting triggered. Any help regarding the issue will be very helpful. Thanks.
P.S. As it is a home project, I can not afford any premium HTML-to-PDF converter
I'm using angular 7 for client-side and had npm packages installed accordingly, which included the html-pdf
package as well. However, I had not installed it in the directory where I have kept the node script. I was hoping that the package would be taken from the vendor.js after deployment, which was clearly not the case. I had to create another package.json file in the directory and installed it separately after deployment and everything went smooth thereafter.
That's what I was missing when deploying the application - a little manual npm install
for installing the package I'm using, in the local directory of the node script.
Thanks a lot for discussing the issue, it helped me a lot in understanding what other mistakes I might have done.