jsreport/jsreport-dotnet-aspnetcore

How to use jsreport-pdf-password with razor views

KhalilMohammad opened this issue · 3 comments

How to use pdf password extension with razor

As instructed here
I have created jsreport.config.json
I have created jsreport and installed both
jsreport-pdf-password
jsreport-phantom-pdf

How can I supply pdf-password config from asp.net core application
I do not have a problem using node from node services.

Sorry for the very late response.

I didn't try this code, but here is the idea how it could work for you.

HttpContext.JsReportFeature()
                .Recipe(Recipe.PhantomPdf)
                .Configure((r) =>
                {
                    r.Overwrites = new
                    {
                        Template = new
                        {
                            pdfPassword = new
                            {
                                active = true,
                                password = "1234"
                            }
                        }
                    };
                });

In the end , I implemented above task using node js. I used node services for that

I used PdfSharp to set a password and modified response to return it a new file stream.

HttpContext.JsReportFeature().Recipe(Recipe.ChromePdf)
                .OnAfterRender((r) => {
                    string destination = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
                    using (var file = System.IO.File.Open(destination, System.IO.FileMode.Create))
                    {
                        r.Content.CopyTo(file);
                    }

                    PdfDocument document = PdfReader.Open(destination);
                    PdfSecuritySettings securitySettings = document.SecuritySettings;
                    securitySettings.UserPassword = "1234";

                    document.Save(destination);

                    HttpContext.Response.Clear();
                    var contentDisposition = "attachment; filename=\"myReport.pdf\"";
                    HttpContext.Response.Headers["Content-Disposition"] = contentDisposition;
                    HttpContext.Response.ClearContent();
                    HttpContext.Response.WriteFile(destination);

                }
);