Skip to content

Commit 45d8887

Browse files
897376_ug: Update azure app function linux samples.
1 parent 97f93f9 commit 45d8887

23 files changed

+151
-490
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.4.32916.344
4+
VisualStudioVersion = 17.9.34622.214
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", "{166CE94E-6280-4546-9ADD-E8BA27C62AC7}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HTML_to_PDF_Azure_functions", "HTML_to_PDF_Azure_functions\HTML_to_PDF_Azure_functions.csproj", "{B865214A-4EB5-4A78-8C5E-2DDBBADA0265}"
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-
{166CE94E-6280-4546-9ADD-E8BA27C62AC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{166CE94E-6280-4546-9ADD-E8BA27C62AC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{166CE94E-6280-4546-9ADD-E8BA27C62AC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{166CE94E-6280-4546-9ADD-E8BA27C62AC7}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{B865214A-4EB5-4A78-8C5E-2DDBBADA0265}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B865214A-4EB5-4A78-8C5E-2DDBBADA0265}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B865214A-4EB5-4A78-8C5E-2DDBBADA0265}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B865214A-4EB5-4A78-8C5E-2DDBBADA0265}.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 = {ADB96CAC-200F-4397-B67F-5FA8234E273D}
23+
SolutionGuid = {B2186101-8624-46F3-9754-46B7DCB83A52}
2424
EndGlobalSection
2525
EndGlobal

Azure/HTML_to_PDF_Azure_functions/HTML_to_PDF_Azure_functions/Function1.cs

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,113 @@
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;
9-
using Newtonsoft.Json;
105
using Syncfusion.HtmlConverter;
116
using Syncfusion.Pdf;
7+
using Syncfusion.Pdf.Graphics;
8+
using System.Reflection;
129
using System.Runtime.InteropServices;
1310

14-
namespace HTML_to_PDF_Azure_functions
11+
namespace FunctionApp_Linux_HTMLtoPDF
1512
{
16-
public static class Function1
13+
public class Function1
1714
{
18-
[FunctionName("Function1")]
19-
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext)
15+
private readonly ILogger<Function1> _logger;
16+
17+
public Function1(ILogger<Function1> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
[Function("Function1")]
23+
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, FunctionContext executionContext)
2024
{
2125
string blinkBinariesPath = string.Empty;
26+
//Create a new PDF document.
27+
PdfDocument document;
28+
BlinkConverterSettings settings = new BlinkConverterSettings();
29+
//Creating the stream object.
30+
MemoryStream stream;
2231
try
2332
{
24-
blinkBinariesPath = SetupBlinkBinaries(executionContext);
33+
blinkBinariesPath = SetupBlinkBinaries();
34+
35+
//Initialize the HTML to PDF converter with the Blink rendering engine.
36+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
37+
38+
//Set command line arguments to run without sandbox.
39+
settings.CommandLineArguments.Add("--no-sandbox");
40+
settings.CommandLineArguments.Add("--disable-setuid-sandbox");
41+
settings.BlinkPath = blinkBinariesPath;
42+
//Assign BlinkConverter settings to the HTML converter
43+
htmlConverter.ConverterSettings = settings;
44+
//Convert URL to PDF
45+
document = htmlConverter.Convert("http://www.syncfusion.com");
46+
stream = new MemoryStream();
47+
//Save and close the PDF document
48+
document.Save(stream);
2549
}
26-
catch
50+
catch(Exception ex)
2751
{
28-
throw new Exception("BlinkBinaries initialization failed");
52+
//Create a new PDF document.
53+
document = new PdfDocument();
54+
//Add a page to the document.
55+
PdfPage page = document.Pages.Add();
56+
//Create PDF graphics for the page.
57+
PdfGraphics graphics = page.Graphics;
58+
59+
//Set the standard font.
60+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
61+
//Draw the text.
62+
graphics.DrawString(ex.Message.ToString(), font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
63+
64+
stream = new MemoryStream();
65+
//Save the document into memory stream.
66+
document.Save(stream);
67+
2968
}
30-
31-
string url = req.Query["url"];
32-
33-
//Initialize the HTML to PDF converter with the Blink rendering engine.
34-
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
35-
BlinkConverterSettings settings = new BlinkConverterSettings();
36-
37-
//Set command line arguments to run without sandbox.
38-
settings.CommandLineArguments.Add("--no-sandbox");
39-
settings.CommandLineArguments.Add("--disable-setuid-sandbox");
40-
41-
settings.BlinkPath = blinkBinariesPath;
42-
43-
//Assign WebKit settings to the HTML converter
44-
htmlConverter.ConverterSettings = settings;
45-
46-
//Convert URL to PDF
47-
PdfDocument document = htmlConverter.Convert(url);
48-
49-
MemoryStream ms = new MemoryStream();
50-
51-
//Save and close the PDF document
52-
document.Save(ms);
69+
5370
document.Close();
54-
55-
ms.Position = 0;
56-
57-
return new FileStreamResult(ms, "application/pdf");
71+
stream.Position = 0;
72+
return new FileStreamResult(stream, "application/pdf");
5873
}
59-
private static string SetupBlinkBinaries(ExecutionContext executionContext)
74+
private static string SetupBlinkBinaries( )
6075
{
61-
string blinkAppDir = Path.Combine(executionContext.FunctionAppDirectory, "BlinkBinariesLinux");
76+
var fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
77+
string path = fileInfo.Directory.Parent.FullName;
78+
string blinkAppDir = Path.Combine(path, @"wwwroot");
6279
string tempBlinkDir = Path.GetTempPath();
6380
string chromePath = Path.Combine(tempBlinkDir, "chrome");
64-
6581
if (!File.Exists(chromePath))
6682
{
67-
6883
CopyFilesRecursively(blinkAppDir, tempBlinkDir);
69-
7084
SetExecutablePermission(tempBlinkDir);
7185
}
7286
return tempBlinkDir;
7387
}
74-
75-
7688
private static void CopyFilesRecursively(string sourcePath, string targetPath)
7789
{
7890
//Create all the directories from the source to the destination path.
7991
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
8092
{
8193
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
8294
}
83-
8495
//Copy all the files from the source path to the destination path.
8596
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
8697
{
8798
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
8899
}
89100
}
90-
91101
[DllImport("libc", SetLastError = true, EntryPoint = "chmod")]
92102
internal static extern int Chmod(string path, FileAccessPermissions mode);
93-
94103
private static void SetExecutablePermission(string tempBlinkDir)
95104
{
96105
FileAccessPermissions ExecutableFilePermissions = FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite | FileAccessPermissions.UserExecute |
97106
FileAccessPermissions.GroupRead | FileAccessPermissions.GroupExecute | FileAccessPermissions.OtherRead | FileAccessPermissions.OtherExecute;
98-
99107
string[] executableFiles = new string[] { "chrome", "chrome_sandbox" };
100-
101108
foreach (string executable in executableFiles)
102109
{
103110
var execPath = Path.Combine(tempBlinkDir, executable);
104-
105111
if (File.Exists(execPath))
106112
{
107113
var code = Function1.Chmod(execPath, ExecutableFilePermissions);
@@ -118,11 +124,9 @@ internal enum FileAccessPermissions : uint
118124
OtherExecute = 1,
119125
OtherWrite = 2,
120126
OtherRead = 4,
121-
122127
GroupExecute = 8,
123128
GroupWrite = 16,
124129
GroupRead = 32,
125-
126130
UserExecute = 64,
127131
UserWrite = 128,
128132
UserRead = 256
Lines changed: 17 additions & 5 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>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
58
</PropertyGroup>
69
<ItemGroup>
7-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
8-
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Linux" Version="20.3.0.58" />
10+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
11+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
12+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
13+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
14+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
15+
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
16+
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
17+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Linux" Version="24.2.9" />
918
</ItemGroup>
1019
<ItemGroup>
1120
<None Update="host.json">
@@ -16,4 +25,7 @@
1625
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
1726
</None>
1827
</ItemGroup>
19-
</Project>
28+
<ItemGroup>
29+
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
30+
</ItemGroup>
31+
</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();

0 commit comments

Comments
 (0)