Skip to content

Commit 7bf11c6

Browse files
Merge pull request #25 from SyncfusionExamples/897376_ug
897376_ug: Update azure app function linux samples.
2 parents 97f93f9 + b3ce765 commit 7bf11c6

File tree

25 files changed

+153
-492
lines changed

25 files changed

+153
-492
lines changed

Azure/HTML-to-PDF-Azure App Service-Windows-CefSharp/HTML-to-PDF-Azure-app-service/HTML-to-PDF-Azure-app-service.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
14-
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Cef.Net.Windows" Version="26.1.35" />
14+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Cef.Net.Windows" Version="*" />
1515
</ItemGroup>
1616

1717
</Project>

Azure/HTML-to-PDF-AzureApp-container/HTML-to-PDF-AzureApp-container/HTML-to-PDF-AzureApp-container.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
1111
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.0" />
1212
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
13-
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Linux" Version="26.2.11" />
13+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Linux" Version="*" />
1414
</ItemGroup>
1515
<ItemGroup>
1616
<None Update="host.json">
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

0 commit comments

Comments
 (0)