【问题标题】:Wkhtmltopdf rotativa ASP.NET Core MVC issuewkhtmltopdf rotativa ASP.NET Core MVC 问题
【发布时间】:2021-01-02 19:56:48
【问题描述】:

我正在使用一个使用 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(): Returned false Exit with code 1,由于未知错误。

我的 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();
            });
        }
    }
}

我将文件保存在&lt;foldername&gt;/&lt;projectname&gt;/bin/Debug/net5.0/Rotativa

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

【问题讨论】:

  • 好吧,我认为你在 linux 的 wkhtmltopdf 包中有这样的内部问题。你试过另一个包吗?我认为IronPdf 可以正常工作,但只有Free development licensing

标签: c# asp.net-mvc asp.net-core rotativa


【解决方案1】:

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

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

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 通过将版本 net 5.0 降级到 3.1 解决了这个问题。并且工作了。
猜你喜欢
  • 2020-05-16
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2012-12-19
相关资源
最近更新 更多