Skip to content

Instantly share code, notes, and snippets.

@GuyHarwood
Forked from tmakin/ExportPdf.cs
Created September 29, 2017 15:50
Show Gist options
  • Save GuyHarwood/f578109244d887874cc1c948c40bd184 to your computer and use it in GitHub Desktop.
Save GuyHarwood/f578109244d887874cc1c948c40bd184 to your computer and use it in GitHub Desktop.
Create PDF Rendering service in Azure Functions

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

<title>Title</title> Body text... "
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
],
"disabled": false
}
{
"frameworks": {
"net46":{
"dependencies": {
"OpenHtmlToPdf": "1.12.0"
}
}
}
}
using System.Net;
using System.Net.Http.Headers;
using OpenHtmlToPdf;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("Processing PDF Request");
string html = await req.Content.ReadAsStringAsync();
var pdf = Pdf
.From(html)
.Content();
log.Info($"PDF Generated. Length={pdf.Length}");
var res = new HttpResponseMessage(HttpStatusCode.OK);
res.Content = new ByteArrayContent(pdf);
res.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment