使用dinktopdf将现有PDF附加到新PDF



我需要将现有的pdf附加到新的pdf,使用dinktopdf,我试图使用或传递base64和文件位置作为src,但没有成功,它生成白色页面,任何人如果它是dinktopdf的任何限制?

还是我做错了什么?

下面的代码,我正在使用测试,谢谢你,如果有人可以帮助。

Obs:运行在docker上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>";

对象设置- HTML内容属性是一个字符串。因此,我假设您有一个文件,另一个文件是从HTML生成的。所以第一个文件(字节数组)转换为字符串并添加到第二个HTML字符串。然后,如果你转换,它会给你一个附加了两个文件内容的文件。

最新更新