U bevindt zich nu op een technische pagina over de software Mercator. Deze pagina bevat specifieke informatie die bestemd is voor professionals van de software Mercator. Wenst u naar algemenere informatie over Mercator door te gaan?


   Deze vraag niet meer stellen

Maak een PDF van een document uit het commercieel beheer

0000002864     -      01-12-2021

De onderstaande code laat zien hoe u een PDF-bestand kan maken en tonen op een ASP.net website. De code kan in een algemene handler (ShowPdf.ashx) geplaatst worden die id, journaal en piece als parameters verwacht. Het voorbeeld wordt hier gegeven voor een factuur (type = 1). Deze programmering maakt gebruik van MercatorReporting, die de PDF genereert in een array van bytes (outputDescriptorExport.FileContent), waardoor het niet meer nodig is om deze vooraf als bestand te bewaren.

De URL voor toegang tot deze pdf heeft volgend formaat : https://www.monsite.com/ShowPdf.ashx?id=A0B1C2D345&journal=Factu&piece=20180298.

Zoom
<%@ WebHandler Language="C#" Class="ShowPdf" %>

using System;
using System.Web;
using MercatorApi;
using System.Collections.Generic;

public class ShowPdf : IHttpHandler, System.Web.SessionState.IRequiresSessionState {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Cache.SetExpires(DateTime.Now.AddHours(1));
        context.Response.Buffer = false;
        context.Response.ContentType = "application/pdf";

        if (string.IsNullOrEmpty(context.Request.Params["id"]) || string.IsNullOrEmpty(context.Request.Params["journal"]) || string.IsNullOrEmpty(context.Request.Params["piece"]))
        {
            context.Response.StatusCode = 500;
            context.Response.End();
            return;
        }

        string id = context.Request.Params["id"];
        string journal = context.Request.Params["journal"];
        Int64 piece = Api.ValSafeInt64(context.Request.Params["piece"]);
        int type = 1;
        using (MercatorUi.Engine.Gescom.BillingEngine billingEngine = MercatorUi.Engine.Gescom.BillingEngine.InitExisting(MercatorUi.Engine.Gescom.Billing.TypeVAEnum.V, type, id, journal, piece))
        {
            if (billingEngine.DataSet == null)
            {
                context.Response.StatusCode = 404;
                context.Response.End();
                return;
            }
            MercatorUi.Reporting.OutputDescriptorExport outputDescriptorExport = new MercatorUi.Reporting.OutputDescriptorExport(MercatorUi.Reporting.ExportReportEnum.PDF, null, MercatorUi.Reporting.ExportOpenAfterEnum.No);
            outputDescriptorExport.ExportToBytes = true;
            List<MercatorUi.Reporting.OutputDescriptor> outputDescriptors = new List<MercatorUi.Reporting.OutputDescriptor>();
            outputDescriptors.Add(outputDescriptorExport);
            System.Data.DataSet dataSetPrintPrev = billingEngine.PrepareDataSetForPrint();
            Dictionary<string, object> parametersPrintPrev = billingEngine.PrepareParametersForPrint();
            Api.LastError = "";
            MercatorUi.Reporting.ReportingStatic.Reporting.RunReport("", billingEngine.SEQUENC["MODELEX_1"].ToString(), dataSetPrintPrev, outputDescriptors, parametersPrintPrev);
            if (!string.IsNullOrEmpty(Api.LastError) || (outputDescriptorExport.FileContent == null) || (outputDescriptorExport.FileContent.Length == 0))
            {
                context.Response.StatusCode = 500;
                context.Response.End();
                return;
            }
            context.Response.AddHeader("Content-Disposition",
                            "attachment; " +
                            "filename=\"" + journal + "_"  + piece.ToString() + ".pdf\"; " +
                            "size=" + outputDescriptorExport.FileContent.Length.ToString() + "; " +
                            "creation-date=" + DateTime.Now.ToString("R").Replace(",", "") + "; " +
                            "modification-date=" + DateTime.Now.ToString("R").Replace(",", "") + "; " +
                            "read-date=" + DateTime.Now.ToString("R").Replace(",", ""));
            context.Response.BinaryWrite(outputDescriptorExport.FileContent);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}