Skip to content

Instantly share code, notes, and snippets.

@tmakin
Last active December 17, 2022 11:29
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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
}
}
});
}
}
}
@teedor
Copy link

teedor commented Apr 20, 2017

Thanks for sharing this. For some reason the pdf I get out of this just shows a bunch of solid squares. Any idea why that might be?

This only happens on azure function, azure webjob, or azure website. When i host in a vm (as web api) it works fine.

image

@vcbDotnet
Copy link

@teedor

I have the same issue seem to be font access limitation. I'm not able to render any text !! Doesn't seem to work in azure function.
https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks

@threadpunter
Copy link

I received the same rendering issue, however was able to solve it. To be able to render the text use a "App Plan" (Basic or Higher) rather than "Consumption Plan" as this allows GDI+, which is required by the PDF converter.

@paulculmsee
Copy link

paulculmsee commented Mar 5, 2018

Ah so close... this method appears to hang when you send it HTML with inline images using the dataURI trick

update moving to an app plan fixed it :-)

@nbayles05
Copy link

nbayles05 commented Mar 13, 2018

This is great information. I was able to get the project working just as you described.

@wjhepworth
Copy link

So I finally got around to trying this and it does work as advertised. Converting the app service plan truly did the trick though as at first I did get the blocks. Great service to the community.

@CaptRonSDPBC
Copy link

I'm sorry if this is really a noobie question, but how do I add this as a Function App in Azure? I found the function app and I created one as an app plan. I get an error that "the host.json file is missing the required 'version' property " . On the Function App Settings page I see the host.json and the only thing in it is { "version": "1.0" }
I n View Files on the function I have the function.json, project.json, and run.cmx files. What am I missing? The tutorials on Azure are terrible.

@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