Skip to content

Instantly share code, notes, and snippets.

@tmakin
Last active December 17, 2022 11:29
Show Gist options
  • Save tmakin/80e1905094245c857c493b7879657095 to your computer and use it in GitHub Desktop.
Save tmakin/80e1905094245c857c493b7879657095 to your computer and use it in GitHub Desktop.
Create PDF Rendering service in Azure Functions

Notes

Testing

To test this function in the azure portal you simply need to post some HTML in the request body. For example:

<!DOCTYPE html>
<html>
<head><meta charset='UTF-8'>
<title>Title</title>
</head>
<body>Body text...</body>
</html>

History

28/04/2020: Updated for azure functions v3 using a different nuget package

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using DinkToPdf;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using IPdfConverter = DinkToPdf.Contracts.IConverter;
namespace ExportFunctionApp
{
public static class ExportPdf
{
private static readonly IPdfConverter _pdfConverter = new SynchronizedConverter(new PdfTools());
[FunctionName("ExportPdf")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing PDF Request");
string html = await req.ReadAsStringAsync();
var pdf = BuildPdf(html);
log.LogInformation($"PDF Generated. Length={pdf.Length}");
var res = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(pdf)
};
res.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
return res;
}
private static byte[] BuildPdf(string html)
{
const double margin = 25;
return _pdfConverter.Convert(new HtmlToPdfDocument()
{
GlobalSettings = {
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings(margin, margin, margin, margin),
},
Objects =
{
new ObjectSettings
{
WebSettings = new WebSettings
{
PrintMediaType = true,
EnableIntelligentShrinking = true
},
HtmlContent = html
}
}
});
}
}
}
@Kamerdracy
Copy link

I'm getting a compilation error:
run.csx(3,7): error CS0246: The type or namespace name 'OpenHtmlToPdf' could not be found (are you missing a using directive or an assembly reference?)

@arunkumaryoyo
Copy link

Is it possible for us to embedd custom font? I tried with base 64, still no luck. Any help would be much appreciated.

Thanks in advance.

@tmakin
Copy link
Author

tmakin commented Oct 15, 2020

Sorry @arunkumaryoyo, it's been a while since I used this code as I've now switched to AWS lambda for pdf rendering. Have you tried using a webfont referenced in the css?

@arunkumaryoyo
Copy link

Sorry @arunkumaryoyo, it's been a while since I used this code as I've now switched to AWS lambda for pdf rendering. Have you tried using a webfont referenced in the css?

I tried and no luck @tmakin

@tmakin
Copy link
Author

tmakin commented Mar 9, 2021

I've added some snippets for an AWS version which may help unblock people having issues with fonts.
https://gist.github.com/tmakin/d391f446588b040665765b0f7e06c240

@christranv
Copy link

Try this library instead. He remake it and I tested it worked well on Azure Function v3
https://github.com/HakanL/WkHtmlToPdf-DotNet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment