jsreport / jsreport-dotnet-aspnetcore

Asp.net core and MVC support for jsreport c# sdk
MIT License
11 stars 8 forks source link

How to use jsreport-pdf-password with razor views #3

Closed KhalilMohammad closed 6 years ago

KhalilMohammad commented 6 years ago

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.

pofider commented 6 years ago

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"
                            }
                        }
                    };
                });
KhalilMohammad commented 6 years ago

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

shaheed-sadi commented 5 years ago

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);

                }
);