简体   繁体   English

如何在 ASP.NET Core 3.1 MVC 中设置 wkhtmltopdf 选项?

[英]How to set wkhtmltopdf options in ASP.NET Core 3.1 MVC?

I am trying to generate a custom PDF using wkhtmltopdf with ASP.NET Core 3.1, here are the docs: https://wkhtmltopdf.org/usage/wkhtmltopdf.txt我正在尝试使用带有 ASP.NET Core 3.1 的 wkhtmltopdf 生成自定义 PDF,这里是文档: httpsk://wkhtmltopdf.txt。

My main problem is: how and where should I put those options?我的主要问题是:我应该如何以及在哪里放置这些选项? Inside cshtml?在cshtml里面? Inside a controller?在 controller 内部? Where?在哪里?

I cannot find documentation over the internet how to use it in ASP.NET Core MVC.我在互联网上找不到如何在 ASP.NET Core MVC 中使用它的文档。

This is my code that is working ok:这是我工作正常的代码:

public async Task<IActionResult> PDF(int? id)
{
        if(id == null)
        {
            return StatusCode(400);
        }

        var comanda = _comandasRepository.Get((int)id);

        if(comanda == null)
        {
            return StatusCode(404);
        }

        return await _generatePdf.GetPdf("Views/Comandas/PDF.cshtml", comanda);
}

This generates a PDF:这会生成一个 PDF:

在此处输入图像描述

But if you notice that PDF has margins, I want to remove these margins.但是如果您注意到 PDF 有边距,我想删除这些边距。 I found in the docs that I have to use --margin-top 0 but I do not know where to put it to make it work.我在文档中发现我必须使用--margin-top 0但我不知道把它放在哪里才能使它工作。

I tried:我试过了:

public async Task<IActionResult> PDF(int? id)
{
    if(id == null)
    {
        return StatusCode(400);
    }

    var comanda = _comandasRepository.Get((int)id);

    if(comanda == null)
    {
        return StatusCode(404);
    }

    return await _generatePdf.GetPdf("--margin-top 0 Views/Comandas/PDF.cshtml", comanda);
}

but does not work, and I tried to put it in a plain html inside cshtml code and didn't work either.但不起作用,我试图将它放在 cshtml 代码中的普通 html 中,但也没有用。 My issue looks so simple but I cannot make this work, how do I solve this?我的问题看起来很简单,但我无法完成这项工作,我该如何解决?

My main problem is: how and where should I put those options?我的主要问题是:我应该如何以及在哪里放置这些选项? Inside cshtml?在cshtml里面? Inside a controller?在 controller 内部? Where?在哪里?

Your shared document is used to teach you how to use command line to do some operation with your pdf.It does not used to work in code behind.您的共享文档用于教您如何使用命令行对您的 pdf 进行一些操作。它不用于在代码后面工作。

But if you notice that PDF has margins, I want to remove these margins.但是如果您注意到 PDF 有边距,我想删除这些边距。 I found in the docs that I have to use --margin-top 0 but I do not know where to put it to make it work.我在文档中发现我必须使用 --margin-top 0 但我不知道把它放在哪里才能使它工作。

You need add the margins by creating ConvertOptions instance:您需要通过创建ConvertOptions实例来添加边距:

public async Task<IActionResult> PDF(int? id)
{
    var options = new ConvertOptions
    {
        PageMargins = new Wkhtmltopdf.NetCore.Options.Margins()
        {
            Top=0
        }
    };

    _generatePdf.SetConvertOptions(options);

    var comanda = _comandasRepository.Get((int)id);

    var pdf = await _generatePdf.GetByteArray("Views/Comandas/PDF.cshtml", comanda);

    var pdfStream = new System.IO.MemoryStream();
    pdfStream.Write(pdf, 0, pdf.Length);
    pdfStream.Position = 0;
    return new FileStreamResult(pdfStream, "application/pdf");
}

More detailed usage you could download the example code on github:更详细的用法可以下载github上的示例代码:

https://github.com/fpanaccia/Wkhtmltopdf.NetCore.Example https://github.com/fpanaccia/Wkhtmltopdf.NetCore.Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM