Skip to content

Commit 274b64d

Browse files
896058: Add HTML to PDF Azure sample.
1 parent 9e95733 commit 274b64d

File tree

14 files changed

+197
-78
lines changed

14 files changed

+197
-78
lines changed

Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.10.35004.147
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HTML-to-PDF-Azure-Functions", "HTML-to-PDF-Azure-Functions\HTML-to-PDF-Azure-Functions.csproj", "{607C0DF5-CA6A-481C-BD55-2FA7AA8C6AFB}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HTML-to-PDF-Azure-Functions", "HTML-to-PDF-Azure-Functions\HTML-to-PDF-Azure-Functions.csproj", "{26FAC187-68EB-4293-85AD-8B64529C7F9D}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{607C0DF5-CA6A-481C-BD55-2FA7AA8C6AFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{607C0DF5-CA6A-481C-BD55-2FA7AA8C6AFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{607C0DF5-CA6A-481C-BD55-2FA7AA8C6AFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{607C0DF5-CA6A-481C-BD55-2FA7AA8C6AFB}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{26FAC187-68EB-4293-85AD-8B64529C7F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{26FAC187-68EB-4293-85AD-8B64529C7F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{26FAC187-68EB-4293-85AD-8B64529C7F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{26FAC187-68EB-4293-85AD-8B64529C7F9D}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
2222
GlobalSection(ExtensibilityGlobals) = postSolution
23-
SolutionGuid = {9E96EB14-82BF-41D9-8B57-78D28B1324A4}
23+
SolutionGuid = {6BD0F272-89B8-47F6-9159-B0835465120D}
2424
EndGlobalSection
2525
EndGlobal
Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
using System;
2-
using System.IO;
3-
using System.Threading.Tasks;
4-
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.Azure.WebJobs;
6-
using Microsoft.Azure.WebJobs.Extensions.Http;
71
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Azure.Functions.Worker;
84
using Microsoft.Extensions.Logging;
95
using Syncfusion.HtmlConverter;
10-
using Syncfusion.Pdf;
11-
using System.Net.Http;
12-
using System.Net;
13-
using System.Net.Http.Headers;
146
using Syncfusion.Pdf.Graphics;
15-
using ExecutionContext = Microsoft.Azure.WebJobs.ExecutionContext;
7+
using Syncfusion.Pdf;
168

179
namespace HTML_to_PDF_Azure_Functions
1810
{
19-
public static class Function1
11+
public class Function1
2012
{
21-
[FunctionName("Function1")]
22-
public static async Task<HttpResponseMessage> Run(
23-
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
24-
ILogger log)
13+
private readonly ILogger<Function1> _logger;
14+
15+
public Function1(ILogger<Function1> logger)
2516
{
17+
_logger = logger;
18+
}
19+
20+
[Function("Function1")]
21+
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
22+
{
23+
2624
MemoryStream ms = new MemoryStream();
27-
string logs = string.Empty;
2825
try
2926
{
3027
//Initialize HTML to PDF converter.
3128
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Cef);
3229
CefConverterSettings settings = new CefConverterSettings();
33-
//logs += settings.
3430
//Assign WebKit settings to HTML converter.
3531
htmlConverter.ConverterSettings = settings;
3632
//Convert URL to PDF.
37-
PdfDocument document = htmlConverter.Convert("https://www.google.com");
33+
PdfDocument document = htmlConverter.Convert("https://www.google.com/");
3834
//Save and close the PDF document.
3935
document.Save(ms);
4036
document.Close();
@@ -63,14 +59,7 @@ public static async Task<HttpResponseMessage> Run(
6359
document.Close(true);
6460
ms.Position = 0;
6561
}
66-
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
67-
response.Content = new ByteArrayContent(ms.ToArray());
68-
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
69-
{
70-
FileName = "Sample.pdf"
71-
};
72-
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
73-
return response;
62+
return new FileStreamResult(ms, "application/pdf");
7463
}
7564
}
7665
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
58
<RootNamespace>HTML_to_PDF_Azure_Functions</RootNamespace>
69
</PropertyGroup>
710
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.0" />
11+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
13+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
14+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
15+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
16+
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
17+
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
918
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Cef.Net.Windows" Version="26.1.41" />
1019
</ItemGroup>
1120
<ItemGroup>
@@ -16,14 +25,8 @@
1625
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1726
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
1827
</None>
19-
<None Update="runtimes\win-x64\native\libeay32.dll">
20-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21-
</None>
22-
<None Update="runtimes\win-x64\native\libssl32.dll">
23-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24-
</None>
25-
<None Update="runtimes\win-x64\native\ssleay32.dll">
26-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27-
</None>
2828
</ItemGroup>
29-
</Project>
29+
<ItemGroup>
30+
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
31+
</ItemGroup>
32+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.Azure.Functions.Worker;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
5+
var host = new HostBuilder()
6+
.ConfigureFunctionsWebApplication()
7+
.ConfigureServices(services =>
8+
{
9+
services.AddApplicationInsightsTelemetryWorkerService();
10+
services.ConfigureFunctionsApplicationInsights();
11+
})
12+
.Build();
13+
14+
host.Run();

Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions/Properties/PublishProfiles/HTML-to-PDF-Azure-Functions20240710153831 - Zip Deploy.pubxml

Lines changed: 0 additions & 24 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project>
6+
<PropertyGroup>
7+
<WebPublishMethod>MSDeploy</WebPublishMethod>
8+
<PublishProvider>AzureWebSite</PublishProvider>
9+
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
10+
<LastUsedPlatform>Any CPU</LastUsedPlatform>
11+
<SiteUrlToLaunchAfterPublish>https://html-to-pdf-azure-sample.azurewebsites.net</SiteUrlToLaunchAfterPublish>
12+
<LaunchSiteAfterPublish>false</LaunchSiteAfterPublish>
13+
<ResourceId>/subscriptions/349260c9-c5a3-4c13-8f1c-7a9f185aa74d/resourcegroups/HTMLToPDFAzureFunction/providers/Microsoft.Web/sites/HTML-to-PDF-Azure-Sample</ResourceId>
14+
<UserName>$HTML-to-PDF-Azure-Sample</UserName>
15+
<_SavePWD>true</_SavePWD>
16+
<ExcludeApp_Data>false</ExcludeApp_Data>
17+
<MSDeployServiceURL>html-to-pdf-azure-sample.scm.azurewebsites.net:443</MSDeployServiceURL>
18+
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
19+
<SkipExtraFilesOnServer>false</SkipExtraFilesOnServer>
20+
<EnableMsDeployAppOffline>true</EnableMsDeployAppOffline>
21+
<EnableMSDeployBackup>true</EnableMSDeployBackup>
22+
<DeployIisAppPath>HTML-to-PDF-Azure-Sample</DeployIisAppPath>
23+
</PropertyGroup>
24+
</Project>

Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions/Properties/ServiceDependencies/HTML-to-PDF-Azure-Functions20240710153831 - Zip Deploy/appInsights1.arm.json renamed to Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions/Properties/ServiceDependencies/HTML-to-PDF-Azure-Sample - Web Deploy/appInsights1.arm.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"parameters": {
55
"resourceGroupName": {
66
"type": "string",
7-
"defaultValue": "HtmlToPdfAzureFunction",
7+
"defaultValue": "HTMLToPDFAzureFunction",
88
"metadata": {
99
"_parameterType": "resourceGroup",
1010
"description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking."
@@ -36,7 +36,7 @@
3636
},
3737
{
3838
"type": "Microsoft.Resources/deployments",
39-
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat('HtmlToPdfAppContainer', subscription().subscriptionId)))]",
39+
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat('HtmltoPDFAzureSample', subscription().subscriptionId)))]",
4040
"resourceGroup": "[parameters('resourceGroupName')]",
4141
"apiVersion": "2019-10-01",
4242
"dependsOn": [
@@ -50,7 +50,7 @@
5050
"resources": [
5151
{
5252
"kind": "web",
53-
"name": "HtmlToPdfAppContainer",
53+
"name": "HtmltoPDFAzureSample",
5454
"type": "microsoft.insights/components",
5555
"location": "[parameters('resourceLocation')]",
5656
"properties": {},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"metadata": {
5+
"_dependencyType": "compute.appService.windows"
6+
},
7+
"parameters": {
8+
"resourceGroupName": {
9+
"type": "string",
10+
"defaultValue": "HTMLToPDFAzureFunction",
11+
"metadata": {
12+
"description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking."
13+
}
14+
},
15+
"resourceGroupLocation": {
16+
"type": "string",
17+
"defaultValue": "eastus",
18+
"metadata": {
19+
"description": "Location of the resource group. Resource groups could have different location than resources, however by default we use API versions from latest hybrid profile which support all locations for resource types we support."
20+
}
21+
},
22+
"resourceName": {
23+
"type": "string",
24+
"defaultValue": "HTML-to-PDF-Azure-Sample",
25+
"metadata": {
26+
"description": "Name of the main resource to be created by this template."
27+
}
28+
},
29+
"resourceLocation": {
30+
"type": "string",
31+
"defaultValue": "[parameters('resourceGroupLocation')]",
32+
"metadata": {
33+
"description": "Location of the resource. By default use resource group's location, unless the resource provider is not supported there."
34+
}
35+
}
36+
},
37+
"variables": {
38+
"appServicePlan_name": "[concat('Plan', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId)))]",
39+
"appServicePlan_ResourceId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', parameters('resourceGroupName'), '/providers/Microsoft.Web/serverFarms/', variables('appServicePlan_name'))]"
40+
},
41+
"resources": [
42+
{
43+
"type": "Microsoft.Resources/resourceGroups",
44+
"name": "[parameters('resourceGroupName')]",
45+
"location": "[parameters('resourceGroupLocation')]",
46+
"apiVersion": "2019-10-01"
47+
},
48+
{
49+
"type": "Microsoft.Resources/deployments",
50+
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId)))]",
51+
"resourceGroup": "[parameters('resourceGroupName')]",
52+
"apiVersion": "2019-10-01",
53+
"dependsOn": [
54+
"[parameters('resourceGroupName')]"
55+
],
56+
"properties": {
57+
"mode": "Incremental",
58+
"template": {
59+
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
60+
"contentVersion": "1.0.0.0",
61+
"resources": [
62+
{
63+
"location": "[parameters('resourceLocation')]",
64+
"name": "[parameters('resourceName')]",
65+
"type": "Microsoft.Web/sites",
66+
"apiVersion": "2015-08-01",
67+
"tags": {
68+
"[concat('hidden-related:', variables('appServicePlan_ResourceId'))]": "empty"
69+
},
70+
"dependsOn": [
71+
"[variables('appServicePlan_ResourceId')]"
72+
],
73+
"kind": "app",
74+
"properties": {
75+
"name": "[parameters('resourceName')]",
76+
"kind": "app",
77+
"httpsOnly": true,
78+
"reserved": false,
79+
"serverFarmId": "[variables('appServicePlan_ResourceId')]",
80+
"siteConfig": {
81+
"metadata": [
82+
{
83+
"name": "CURRENT_STACK",
84+
"value": "dotnetcore"
85+
}
86+
]
87+
}
88+
},
89+
"identity": {
90+
"type": "SystemAssigned"
91+
}
92+
},
93+
{
94+
"location": "[parameters('resourceLocation')]",
95+
"name": "[variables('appServicePlan_name')]",
96+
"type": "Microsoft.Web/serverFarms",
97+
"apiVersion": "2015-08-01",
98+
"sku": {
99+
"name": "S1",
100+
"tier": "Standard",
101+
"family": "S",
102+
"size": "S1"
103+
},
104+
"properties": {
105+
"name": "[variables('appServicePlan_name')]"
106+
}
107+
}
108+
]
109+
}
110+
}
111+
}
112+
]
113+
}

Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions/Properties/ServiceDependencies/HTML-to-PDF-Azure-Functions20240710153831 - Zip Deploy/storage1.arm.json renamed to Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions/Properties/ServiceDependencies/HTML-to-PDF-Azure-Sample - Web Deploy/storage1.arm.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"parameters": {
55
"resourceGroupName": {
66
"type": "string",
7-
"defaultValue": "HtmlToPdfAzureFunction",
7+
"defaultValue": "HTMLToPDFAzureFunction",
88
"metadata": {
99
"_parameterType": "resourceGroup",
1010
"description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking."
@@ -36,7 +36,7 @@
3636
},
3737
{
3838
"type": "Microsoft.Resources/deployments",
39-
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat('htmltopdfazurefuncta9c7', subscription().subscriptionId)))]",
39+
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat('htmltopdfazurefunct9c88', subscription().subscriptionId)))]",
4040
"resourceGroup": "[parameters('resourceGroupName')]",
4141
"apiVersion": "2019-10-01",
4242
"dependsOn": [
@@ -54,7 +54,7 @@
5454
"tier": "Standard"
5555
},
5656
"kind": "Storage",
57-
"name": "htmltopdfazurefuncta9c7",
57+
"name": "htmltopdfazurefunct9c88",
5858
"type": "Microsoft.Storage/storageAccounts",
5959
"location": "[parameters('resourceLocation')]",
6060
"apiVersion": "2017-10-01"

Azure/HTML-to-PDF-Azure-Function-Windows-CefSharp/HTML-to-PDF-Azure-Functions/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"HTML_to_PDF_Azure_Functions": {
44
"commandName": "Project",
5-
"commandLineArgs": "--port 7253",
5+
"commandLineArgs": "--port 7061",
66
"launchBrowser": false
77
}
88
}

0 commit comments

Comments
 (0)