Sunday, January 16, 2022

Convert word stream to PDF file X++ in D365FO

 using AK = Microsoft.Dynamics365.LocalizationFramework.MicrosoftInternalUseOnly.ApiKeys;

using GR = Microsoft.Dynamics365.LocalizationFramework.Repositories.GlobalRepository;

using Microsoft.Dynamics365.LocalizationFramework;

using TL = Microsoft.Dynamics365.LocalizationFramework.Telemetry;

using BCS = Microsoft.Dynamics365.LocalizationFramework.ExternalApi.BCS;

using EA = Microsoft.Dynamics365.LocalizationFramework.ExternalApi;

using Microsoft.Dynamics.ApplicationPlatform.Environment;

using Microsoft.Dynamics.ApplicationPlatform.Environment.Settings;

using Microsoft.Dynamics.AX.Security.AuthenticationCommon;

using Microsoft.Dynamics.AX.Security.ADALClientAuthentication;

using Microsoft.Dynamics.AX.Framework.FileManagement;

class WordToPDFConverter 

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>


    public static void main(Args _args)

    {

        System.IO.Stream inputStream; //word stream

        str fileExtension = '.docx';

        System.IO.Stream outputPDFStream; //Pdf stream

        System.IO.Stream    fileStream;

        try

        {

            fileStream  = File::UseFileFromURL('C:\\wordfile\\word1.docx');

        }

        catch

        {

            warning("File not found");

        }

        inputStream =  fileStream;

        BCS.ConvertToPdfRequest request = new BCS.ConvertToPdfRequest();

        BCS.ConvertToPdfResponse response;


        request.FileStream = inputStream;

        request.ClientCorrelationId = newGuid();

        request.ClientName = identifierStr(DynamicsAxElectronicReporting);

        request.FileExtension = fileExtension;


        var ret = new BCS.PdfConversionPageOptions();

        ret.Orientation = BCS.PdfConversionPageOrientation::Portrait;


        request.PageOptions = ret;

        request.LocaleIdentifier = 0;


        AK.IApiKeysProviderService apiKeysProvider = null;

        Microsoft.Dynamics.Client.ServerForm.Contexts.SessionContext sessionContext;

        System.Uri currentUrl = null;

        sessionContext = Microsoft.Dynamics.Client.ServerForm.Contexts.SessionContext::get_Current();

        if (sessionContext)

        {

            currentUrl = sessionContext.get_RequestUrl();

        }

        str ProdUrl = 'https://apikeys.configure.global.dynamics.com';

        System.String configValue = System.Configuration.ConfigurationManager::AppSettings.Get('RCS.GlobalRepoKeysAPIUrl');

        var configProvider = System.String::IsNullOrEmpty(configValue) ? ProdUrl : configValue;

        str authProvd = ERParameterTable::find().RCSApplicationAADUrl;

        if (!authProvd)

        {

            authProvd = (ERParameterTable::isMooncake() ? 'https://configure.global.dynamics.cn' : 'http://configure.global.dynamics.com');

        }

            var s2SAuthProvider = new GR.S2SAuthenticationProvider(authProvd);

            apiKeysProvider = new GR.MicrosoftInternalUseOnly.GlobalRepositoryApiKeysClient(s2SAuthProvider, configProvider);

            var provider = apiKeysProvider;

            var conversionClient = EA.BCS.BcsApiClient::Instance;

            response =  conversionClient.ConvertToPdf(request, provider, TL.ElectronicReportingMappingTelemetryLogger::Log);

            if (response.IsValidResponse)

            {

                outputPDFStream = response.ResultStream;

                File::SendFileToUser(outputPDFStream,'word1.pdf');

            }        

    }

}


2 comments:

  1. I hope this message finds you well. I am currently working on a Dynamics 365 Finance and Operations project and have encountered two errors that I am seeking assistance with. I believe your expertise in this area could be valuable in resolving these issues.

    Error 1: Class 'GR.MicrosoftInternalUseOnly.GlobalRepositoryApiKeysClient' Not Found

    Description: This error suggests that the specified class is not available. I have ensured that the necessary references are added to the project, but the class still cannot be found.
    Error 2: Method 'ConvertToPdf' Not Found on Type 'Microsoft.Dynamics365.LocalizationFramework.ExternalApi.BCS.BcsApiClient'

    Description: This error indicates that the mentioned method is not available on the specified type. Despite adding references and configurations, the compiler still cannot locate this method.
    I have reviewed the documentation and setup steps, but these issues persist. I am reaching out to seek your guidance on resolving these errors. If you have any insights, suggestions, or if you could point me to the right resources, I would greatly appreciate it.

    Your assistance on this matter would be invaluable, and I am eager to learn from your expertise. Please let me know if there is a convenient time for you to discuss this further, or if you prefer another mode of communication.

    Thank you in advance for your time and assistance.

    ReplyDelete
  2. add referece to Electronic Reporting

    ReplyDelete

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...