微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wkhtmltopdf rotativa ASP.NET Core MVC 问题

如何解决wkhtmltopdf rotativa ASP.NET Core MVC 问题

我正在使用使用 Ubuntu 18.04 LTS 的数字海洋水滴,并在其上部署了我的 ASP.NET Core 5.0 MVC 项目,一切正常。我试图在其上安装 rotativa 以打印 pdf 文档,但我无法完成这项工作。

我按照以下步骤操作:https://blog.elmah.io/generate-a-pdf-from-asp-net-core-for-free/

但是在尝试打印 pdf 时出现此错误

异常:QPainter::begin():由于未知错误,返回错误退出代码为 1。

enter image description here

我的 Startup.cs 中有这个:

using Wkhtmltopdf.NetCore;

namespace farmamest
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // services.AddControllersWithViews();
            services.AddRazorPages();
            services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.KNownProxies.Add(IPAddress.Parse("10.0.0.100"));
            });

            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<Context>();
            services.AddControllersWithViews();

            services.Configure<IdentityOptions>(options =>
              {
                  // Password settings.
                  options.Password.requiredigit = false;
                  options.Password.RequireLowercase = false;
                  options.Password.RequireNonAlphanumeric = false;
                  options.Password.RequireUppercase = false;
                  options.Password.requiredLength = 6;
                  options.Password.requiredUniqueChars = 1;

                  // Lockout settings.
                  options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
                  //options.Lockout.MaxFailedAccessAttempts = 5;
                  options.Lockout.AllowedForNewUsers = true;

                  // Default SignIn settings.
                  options.SignIn.RequireConfirmedEmail = false;
                  options.SignIn.RequireConfirmedPhoneNumber = false;

                  // User settings.
                  options.User.AllowedUserNameCharacters =
                    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMnopQRSTUVWXYZ0123456789-._@+";
                  options.User.RequireUniqueEmail = true;

              });

            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
               // options.Cookie.Name = Configuration["CookieName"];
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

                options.LoginPath = "/Identity/Account/Login";
                options.AccessDeniedpath = "/Identity/Account/AccessDenied";
                options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                options.SlidingExpiration = true;
            });

            

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
            // options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc(config =>
            {
                // var policy = new AuthorizationPolicyBuilder()
                //                 .RequireAuthenticatedUser()
                //                 .Build();
                // config.Filters.Add(new Authorizefilter(policy));
            });

            services.Configure<PasswordHasherOptions>(option =>
            {
                option.IterationCount = 12000;
            });

            services.AddWkhtmltopdf("Rotativa");

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                // app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            //cookie policy
             app.UseCookiePolicy();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseDefaultFiles();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }
    }
}

我将文件保存在 <foldername>/<projectname>/bin/Debug/net5.0/Rotativa

enter image description here

我如何使这项工作?这个版本的网络问题相关吗?我用的是net5.0,网上找不到解决办法,头疼。

解决方法

这是 .NET 5 的问题。看起来与调用 Wkhtmltopdf 相关。

https://github.com/fpanaccia/Wkhtmltopdf.NetCore/issues/46

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。