我需要附加一个现有的pdf到一个新的pdf,使用dinktopdf,我正在尝试使用或传递base64和文件位置作为src,但没有成功,它生成了白色的页面,如果这是dinktopdf的任何限制?
还是我做错了什么?
下面这段代码是我用来测试的,如果有人能帮上忙,谢谢。
Obs:在坞站上运行:DockerFile csproj config
c#代码:
启动> ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
string wkHtmlToPdfFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "libwkhtmltox.so" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "libwkhtmltox.dylib" :
"libwkhtmltox.dll";
var context = new CustomAssemblyLoadContext();
context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), wkHtmlToPdfFileName));
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
}
protected IConfigurationGenerateFiles _configFiles;
protected IConverter _converter;
private HtmlToPdfDocument _pdfDocument;
protected List<ObjectSettings> _pages = new List<ObjectSettings>();
public DinkToPdfService(IConfigurationGenerateFiles configFiles,
IConverter converter)
{
_configFiles = configFiles;
_converter = converter;
}
public virtual async Task<byte[]> Generate()
{
var globalSettings = new GlobalSettings
{
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
};
_pdfDocument = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
};
await CreateNewPdfDocumentStreamWithDownload(
_configFiles._downloadFile,
"testFileDownload");
_pdfDocument.Objects.AddRange(_pages);
return _converter.Convert(_pdfDocument);
}
protected async Task CreateNewPdfDocumentStreamWithDownload(DownloadFile _downloadFile, string fileId)
{
using (var srcPdfStream = await GetSourcePdfDownloadedStream(_downloadFile, fileId))
{
var objectSettings = new ObjectSettings
{
PagesCount = true,
WebSettings = { DefaultEncoding = "utf-8" },
HtmlContent = HTML_TEST(Convert.ToBase64String(srcPdfStream.ToArray()))
};
_pages.Add(objectSettings);
}
}
private string HTML_TEST(string base64Content) =>
@$"<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style>
table, th, td {{
border: 1px solid black;
border-collapse: collapse;
font-family: Helvetica;
}}
</style>
</head>
<body>
<iframe
src=""data:application/pdf;base64,{base64Content}"" type='application/pdf'
frameBorder='0'
scrolling='auto'
height='700px'
width='100%'>
</iframe>
</body>
</html>";
发布于 2021-12-03 15:47:43
对象设置- HTML内容属性是一个字符串。所以我假设你有一个文件,另一个文件是从HTML生成的。因此,将第一个文件(字节数组)转换为字符串并附加到第二个HTML字符串。然后,如果您进行转换,它会给出一个附加了两个文件内容的文件。
https://stackoverflow.com/questions/69365188
复制相似问题