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 ;
71using Microsoft . AspNetCore . Http ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using Microsoft . Azure . Functions . Worker ;
84using Microsoft . Extensions . Logging ;
9- using Newtonsoft . Json ;
105using Syncfusion . HtmlConverter ;
116using Syncfusion . Pdf ;
7+ using Syncfusion . Pdf . Graphics ;
8+ using System . Reflection ;
129using 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