How to convert Word document to PDF in Azure App service on Linux?
Syncfusion Essential DocIO is a WinForms File Format used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to PDF in Azure App Service on Linux.
Steps to convert Word document to PDF in Azure App service on Linux:
- Create a new ASP.NET Core application.
- Select an MVC project.
- Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org.
- Install the following NuGet packages as a reference to your .NET Core application from NuGet.org.
- Create a Font folder in the “wwwroot” folder of the project and copy the required fonts and include the files to the project.
- Set the copy to output directory to Copy if newer to all the data files.
- Add an Export To PDF button in index.cshtml.
@{Html.BeginForm("WordToPDF", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }); { <div class="Common"> <div class="tablediv"> <div class="rowdiv"> This sample illustrates how to convert Word document to PDF using Essential DocIO and Essential PDF. </div> <div class="rowdiv" style="border-width: 0.5px;border-style:solid; border-color: lightgray; padding: 1px 5px 7px 5px"> Click the button to view the resultant PDF document being converted from Word document using Essential DocIO and Essential PDF. Please note that PDF viewer is required to view the resultant PDF. <div class="rowdiv" style="margin-top: 10px"> <div class="celldiv"> Select Document : @Html.TextBox("file", "", new { type = "file", accept = ".doc,.docx,.rtf,.dot,.dotm,.dotx,docm,.xml" }) <br /> </div> <div class="rowdiv" style="margin-top: 8px"> <input class="buttonStyle" type="submit" value="Convert to PDF" name="button" style="width:150px;height:27px" /> <br /> <div class="text-danger"> @ViewBag.Message </div> </div> </div> </div> <br /> <div class="rowdiv" style="margin-top: 15px"> More information about Word to PDF conversion can be found in this <a href="https://help.syncfusion.com/file-formats/docio/conversion#converting-word-document-to-pdf">documentation</a> section. </div> </div> </div> Html.EndForm(); } }
- Include the following namespaces and code snippet in controller for converting Word to PDF.
// [C# Code] using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocIORenderer; using Syncfusion.Pdf; using Microsoft.AspNetCore.Hosting; using System.IO;
// [C# Code] private IHostingEnvironment _env; public HomeController(IHostingEnvironment env) { _env = env; } /// <summary> /// Convert Word document to PDF /// </summary> /// <param name="button"></param> /// <returns></returns> public IActionResult WordToPDF(string button) { string fileLoadTime = ""; string domLoadTime = ""; string conversionTime = ""; string saveTime = ""; if (button == null) return View("Index"); if (Request.Form.Files != null) { if (Request.Form.Files.Count == 0) { ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document"); return View("Index"); } // Gets the extension from file. string extension = Path.GetExtension(Request.Form.Files[0].FileName).ToLower(); // Compares extension with supported extensions. if (extension == ".doc" || extension == ".docx" || extension == ".dot" || extension == ".dotx" || extension == ".dotm" || extension == ".docm" || extension == ".xml" || extension == ".rtf") { MemoryStream stream = new MemoryStream(); Request.Form.Files[0].CopyTo(stream); try { //Open using Syncfusion WordDocument document = new WordDocument(stream, Syncfusion.DocIO.FormatType.Automatic); stream.Dispose(); stream = null; //Hooks the font substitution event document.FontSettings.SubstituteFont += FontSettings_SubstituteFont; // Creates a new instance of DocIORenderer class. DocIORenderer render = new DocIORenderer(); // Converts Word document into PDF document. PdfDocument pdf = render.ConvertToPDF(document); //Unhooks the font substitution event after converting to PDF document.FontSettings.SubstituteFont -= FontSettings_SubstituteFont; MemoryStream memoryStream = new MemoryStream(); // Save the PDF document pdf.Save(memoryStream); memoryStream.Position = 0; ViewBag.OS = string.Format(System.Environment.OSVersion.ToString()); ViewBag.Load = string.Format("FileLoadTime\t" + fileLoadTime); ViewBag.DomLoad = "DomLoadTime\t" + domLoadTime; ViewBag.Conversion = "ConversionTime\t" + conversionTime; ViewBag.Save = "SaveTime\t" + saveTime; return File(memoryStream, "application/pdf", "WordToPDF.pdf"); } catch (Exception ex) { ViewBag.Message = ex.ToString(); } } else { ViewBag.Message = string.Format("Please choose Word format document to convert to PDF"); } } else { ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document"); } return View("Index"); } /// <summary> /// Sets the alternate font when a specified font is not installed in the production environment /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) { string filePath = string.Empty; FileStream fileStream = null; //Sets the alternate font when a specified font is not installed in the production environment if (args.OriginalFontName == "Calibri") { filePath = _env.WebRootPath + @"/Fonts/calibri.ttf"; fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); args.AlternateFontStream = fileStream; } else if (args.OriginalFontName == "Arial") { filePath = _env.WebRootPath + @"/Fonts/arial.ttf"; fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); args.AlternateFontStream = fileStream; } else { filePath = _env.WebRootPath + @"/Fonts/times.ttf"; fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); args.AlternateFontStream = fileStream; } }
Refer to the following steps to publish as Azure App Linux:
- Right-click the project and select Publish.
- Create a new profile in publish target window.
- Create App service using Azure subscription and select a hosting plan.
- The Syncfusion DocIO library works from basic hosting plan (B1). So, select the required hosting plan. It does not work if the hosting plan is Free/Shared.
- After creating a profile, click the publish button.
- Now, the published webpage will open in the browser. Select the Word document and Click Convert to PDF to convert the given Word document to a PDF.
A complete work sample for converting an Word document to PDF in Azure App service on Linux can be downloaded from AzureAppServiceOnLinux
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to convert Word document to PDF in Azure App service on Linux.
You can refer to our WinForms File Format feature tour
page to know about its other groundbreaking feature representations and documentation, and how to
quickly get started for configuration specifications. You can also explore
our WinForms example to understand how to create and manipulate data.
For current customers, you can check out our Document Processing Libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-trac, or feedback portal. We are always happy to assist you!