Search code examples
asp.net-core-3.1apache-fop

generate pdf file using fop in .net core 3.1


To generate pdf file from fop file I Used FOP.dll and It work well in .net 4.5

  public string GeneratePdf(string foFile, string pdfFile)
{
    java.io.OutputStream os = new java.io.BufferedOutputStream(new java.io.FileOutputStream(new java.io.File(pdfFile)));
        string ret = "";
        try
        {
            org.apache.fop.apps.FopFactory fopFactory = org.apache.fop.apps.FopFactory.newInstance();
            fopFactory.setUserConfig(new java.io.File("fo-config.xml"));
            org.apache.fop.apps.Fop fop = fopFactory.newFop("application/pdf", os);
            org.apache.fop.apps.FOUserAgent foUserAgent = fop.getUserAgent();
            javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance();
            javax.xml.transform.Transformer transformer = factory.newTransformer();
            javax.xml.transform.Source src = new javax.xml.transform.stream.StreamSource(new java.io.File(foFile));
            javax.xml.transform.Result res = new javax.xml.transform.sax.SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);
            ret = pdfFile;
        }
    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        os.close();
    }
    return ret;
}

but when Convert my Code To .net Core 3.1, It Not Working.
java.io.File throw exception: "System.TypeInitializationException: 'The type initializer for 'java.io.File' threw an exception".
the Inner Exception Is :"Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified"
How can I convert a FOP file to PDF exclusively using .netcore 3.1


Solution

  • I have created an open-source pure port of Apache FOP version 2.8 that runs in .Net Core. Check it out:

    FOP.NetCore
    https://github.com/sorcerdon/FOP.NetCore

    Here is the nuget:

    NuGet version (FOP.NetCore)

    Since this is a pure port, that means that it can do anything that Apache FOP can do.