c# - 该程序被 Asp.net core 2.0 中的组策略阻止,使用 Wkhtmltopdf.NetCore
问题描述
我的项目在 Asp.net core 2.0 中并托管在 godaddy windows 共享服务器中。我正在尝试在 wwwroot/images 中生成 pdf 文件。在本地主机文件正在创建和下载,但在服务器上上传后我收到此错误
"该程序被组策略阻止。有关更多信息,请联系您的系统管理员', 'System.ComponentModel.Win32Exception (0x80004005):此程序被组策略阻止。有关更多信息,请
通过 Wkhtmltopdf.NetCore 联系您的系统管理员。 WkhtmlDriver.Convert(String wkhtmlPath, String switch, String html) "
我已经检查了文件夹权限,所有权限都被授予完全控制权,但仍然出现此错误。我正在分享我的代码。请帮助我。
网络配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- To customize the asp.net core module uncomment and edit the following section.
For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
<system.webServer>
<handlers>
<remove name="aspNetCore" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="ASPNETCORE_HOSTINGSTARTUPASSEMBLIES" value="Microsoft.AspNetCore.ApplicationInsights.HostingStartup" />
<environmentVariable name="WEBCONNECTION_DETAILEDERRORS" value="1" />
<environmentVariable name="WEBCONNECTION_USELIVERELOAD" value="False" />
<environmentVariable name="WEBCONNECTION_OPENBROWSER" value="False" />
<environmentVariable name="WEBCONNECTION_SHOWURLS" value="False" />
<environmentVariable name="COMPLUS_ForceENC" value="1" />
</environmentVariables>
</aspNetCore>
</system.webServer>
<system.web>
<trust level="Full" originUrl=""/>
</system.web>
</configuration>
创建文件代码
[Route("CreateFile")]
[HttpPost]
public async Task<JsonResult> CreateFile(OnlineFormModel model)
{
string filePaths = Path.Combine(Directory.GetCurrentDirectory(), $"
{webHostEnvironment.WebRootPath}/images", "OnlineForm.pdf");
try
{
if (!string.IsNullOrEmpty(model.HtmlData))
{
try
{
byte[] pdfData = GenerateFormPdf(model);
Stream stream = new MemoryStream(pdfData);
using (var fileStream = new FileStream(filePaths, FileMode.Create))
{
stream.CopyTo(fileStream);
}
Global.downloadFilePath = "images/OnlineForm.pdf";
}
catch (Exception ex)
{
Global.SaveError(ex);
TempData["Message"] = ex.ToString();
}
}
}
catch (Exception ex)
{
Global.SaveError(ex);
TempData["Message"] = ex.ToString();
}
return Json(new Item { Name = filePaths });
}
---生成pdf方法
public byte[] GenerateFormPdf(OnlineFormModel model)
{
try
{
var data= _generatepdf.GetPDF(model.HtmlData);
return data;
}
catch (Exception ex)
{
Global.SaveError(ex);
TempData["Message"] = ex.ToString();
}
return null;
}
上面的错误是 var data= _generatepdf.GetPDF(model.HtmlData);
Getpdf 是 Wkhtmltopdf 的预定义函数
csproj 代码
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
<PreserveCompilationContext>true</PreserveCompilationContext>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
<!--<AspNetCoreModuleName>AspNetCoreModuleV2</AspNetCoreModuleName>-->
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
<RazorCompileOnPublish>false</RazorCompileOnPublish>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Controllers\DMAController.cs" />
</ItemGroup>
<ItemGroup>
<Content Remove="Views\Home\Index1.cshtml" />
<Content Remove="Views\Home\Index2.cshtml" />
<Content Remove="wwwroot\css\style1.css" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.4" />
<PackageReference Include="iTextSharp.LGPLv2.Core" Version="1.7.3" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Antiforgery" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" />
<PackageReference Include="MySql.Data" Version="8.0.25" />
<PackageReference Include="Select.HtmlToPdf.NetCore" Version="21.1.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="Wkhtmltopdf.NetCore" Version="2.0.1" />
<!--<Content Include=""></Content>-->
<!--<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" PrivateAssets="All" />-->
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>
<ItemGroup>
<None Include="wwwroot\images\banner1.svg" />
<None Include="wwwroot\images\banner2.svg" />
<None Include="wwwroot\images\banner3.svg" />
<None Include="wwwroot\images\banner4.svg" />
<None Include="wwwroot\js\site.js" />
<None Include="wwwroot\js\site.min.js" />
</ItemGroup>
<ItemGroup>
<Folder Include="logs\" />
</ItemGroup>
<ItemGroup>
<None Update="Rotativa\Windows\wkhtmltopdf.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
解决方案
您的错误的原因是 Wkhtmltopdf.NetCore 实际上正在运行一个可执行应用程序,显然,它在 Godaddy Windows 服务器中是不允许的。
这就是为什么在 Wkhtmltopdf.NetCore github 页面中提到您需要这些文件来运行示例。
该结构将需要在您的项目的文件夹中
.
├── Example
| ├── Example.csproj
| └── Rotativa
| | ├── Linux
| | | └── wkhtmltopdf
| | ├── Mac
| | | └── wkhtmltopdf
| | └── Windows
| | └── wkhtmltopdf.exe
└── Example.sln
这些文件将需要包含在您的项目中,并具有“始终复制”属性
推荐阅读
- c - C程序在给出正确的输出后没有结束
- typescript - 打字稿中的故事书配置
- php - 多张图片上传不插入数据
- python - unable to do makemigrations command
- python - 如何在 plotly scattergeo 中自定义颜色条?
- docker - 将 Makefile 移植到 Jenkinsfile,但存在环境变量问题
- python - 如何用随机值刷新字典?
- sql - 有没有办法让我将这些行转换为 oracle 中的列?
- portable-executable - 如何计算导出表的内存虚拟地址的文件偏移量?
- javascript - 在 JavaScript 中,在没有默认参数的其他参数之前为某些参数定义具有默认参数的函数有什么意义?