首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用dinktopdf将现有的pdf附加到新的pdf

使用dinktopdf将现有的pdf附加到新的pdf
EN

Stack Overflow用户
提问于 2021-09-28 16:16:55
回答 1查看 83关注 0票数 0

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

还是我做错了什么?

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

Obs:在坞站上运行:DockerFile csproj config

c#代码:

启动> ConfigureServices

代码语言:javascript
运行
复制
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()));
}
代码语言:javascript
运行
复制
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>";
EN

回答 1

Stack Overflow用户

发布于 2021-12-03 15:47:43

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69365188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档