Page 1 of 1

Docx4j.NET (Access is denied)

PostPosted: Thu Oct 20, 2016 2:24 am
by andrei
HI, i'm using Docx4j v3.0.1 inside a .NET Web API project, which will be hosted on Azure App Services(so, no VMs with Windows installed orwith installed fonts, just a plain FTP). Because of that, i went the 'BestMatchingMapper()' way and after embedding the font into my .docx document, i've edited the docx4j.properties file and set the
Code: Select all
docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart.tmpFontDir = D:\\home\\site\\wwwroot\\temp\\
(while it looks weird, this is the default path of Azure Web apps in which the project is deployed). After this, when trying to convert the docx file the server returns the following error:

Code: Select all
The type initializer for 'org.docx4j.fonts.BestMatchingMapper' threw an exception.","InnerException":{"ClassName":"java.lang.RuntimeException","Message":"org.docx4j.fonts.fop.apps.FOPException: .docx4j (Access is denied)\njava.io.FileNotFoundException: .docx4j (Access is denied)","Data":null,"InnerException":{"ClassName":"org.docx4j.fonts.fop.apps.FOPException","Message":"","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"   at org.docx4j.fonts.fop.util.LogUtil.handleException(Logger log, Exception e, Boolean strict)\r\n   at org.docx4j.fonts.fop.fonts.FontCache.saveTo(File cacheFile)\r\n   at org.docx4j.fonts.fop.fonts.FontCache.save()\r\n   at org.docx4j.fonts.PhysicalFonts.discoverPhysicalFonts()\r\n   at org.docx4j.fonts.BestMatchingMapper..cctor()","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nhandleException\ndocx4j, Version=3.2.0.1, Culture=neutral, PublicKeyToken=null\norg.docx4j.fonts.fop.util.LogUtil\nVoid handleException(org.slf4j.Logger, java.lang.Exception


I don't think it's a simple 'write permission' problem, because i have other code that writes files inside the wwwroot directory and it works fine.

My code:
Code: Select all
using System.IO;
using java.io;
using org.docx4j.fonts;
using org.docx4j.openpackaging.packages;

public static MemoryStream ConvertDocxToPdf(MemoryStream docxToBeConverted)
        {
            java.io.ByteArrayOutputStream fos = new java.io.ByteArrayOutputStream();
            // Necessary, if slf4j-api and slf4j-NetCommonLogging are separate DLLs
            ikvm.runtime.Startup.addBootClassPathAssembly(
                System.Reflection.Assembly.GetAssembly(
                    typeof(org.slf4j.impl.StaticLoggerBinder)));
            ikvm.runtime.Startup.addBootClassPathAssembly(
                System.Reflection.Assembly.GetAssembly(
                    typeof(org.slf4j.LoggerFactory)));

            ikvm.runtime.Startup.addBootClassPathAssembly(
                System.Reflection.Assembly.GetAssembly(
                    typeof(org.docx4j.jaxb.Context)));

            // Configure to find docx4j.properties
            // .. add as URL the dir containing docx4j.properties (not the file itself!)
            Plutext.PropertiesConfigurator.setDocx4jPropertiesDir("D:\\home\\site\\wwwroot\\bin");
            byte[] docByteArray;
            using (docxToBeConverted)
            {
                docByteArray = docxToBeConverted.ToArray();
            }
            InputStream myInputStream = new ByteArrayInputStream(docByteArray);
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                .load(myInputStream);
            wordMLPackage.setFontMapper(new BestMatchingMapper());
            org.docx4j.Docx4J.toPDF(wordMLPackage, fos);

            return new MemoryStream(fos.toByteArray());
        }


Please let me know if you have any idea on how to approach this issue, it would be highly appreciated.

Re: Docx4j.NET (Access is denied)

PostPosted: Thu Aug 02, 2018 1:04 am
by alexmurat
Hello Andrei,

I solved giving access to the users of ISS to modify the path: (Windows Server)

C:\Windows\system32\config\systemprofile

The home path in my local machine is User\My User, but in the server the path is that in the system32 folder.

string home = java.lang.System.getProperty("user.home"); // return the home path

I hope that help you.