0

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

还是我做错了什么?

下面这段代码是我用来测试的,如果有人能帮忙,谢谢。

Obs:在 docker 上运行: DockerFile csproj config

c#代码:

启动 > 配置服务

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>";
4

1 回答 1

0

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

于 2021-12-03T15:47:43.393 回答