捕获和转换Web的工具

如何在DOCX和PDF文档中添加页眉和页脚?

排气歧管

要将页眉和/或页脚添加到DOCX或PDF文档中,首先需要 创建一个模板,并带有指定的标识符。 在这种情况下 report。 该模板可以包括您要显示的所有文本,图像和表格,以及何时显示页眉和页脚的规则。

此外,模板可以包含内置变量,例如页码。 或自定义模板变量,如下所示。 例如,这可能是标题的文本, {number} 是一个自定义变量。

Report number: {number}

然后,您可以将此模板标识符传递给GrabzIt,以将指定的标头添加到任何DOCX或PDF文档中。 在下面显示的示例中,对于我们支持的每种编程语言,上面的标题都显示在每个生成的PDF文档中。 但是,此代码可以很容易地修改为在DOCX文档中添加页眉和页脚。

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
PDFOptions options = new PDFOptions();
options.TemplateId = "report";
options.MarginTop = 50;
options.MarginBottom = 50;
options.AddTemplateParameter("number", "12345");

grabzIt.URLToPDF("http://www.spacex.com", options);
grabzIt.Save("http://www.example.com/Home/Index");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
PDFOptions options = new PDFOptions();
options.setTemplateId("report");
options.setMarginTop(50);
options.setMarginBottom(50);
options.AddTemplateParameter("number", "12345");

grabzIt.URLToPDF("http://www.spacex.com", options);
grabzIt.Save("http://www.example.com/handler");
<script src="https://cdn.jsdelivr.net/npm/@grabzit/js@3.3.7/grabzit.min.js"></script>
<script>
GrabzIt("Sign in to view your Application Key").AddTemplateVariable("number", "12345").ConvertURL("http://www.spacex.com", 
{"format": "pdf", 
"mtop":50, 
"mbottom":50, 
"templateid":"report"}).Create();
</script>
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");
var options = {"marginTop":50,"marginBottom":50,"templateId":"report",
"templateVariables":"number=12345"};

client.url_to_pdf("http://www.spacex.com", options);
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret");
$options = GrabzItPDFOptions->new();
$options->marginTop(50);
$options->marginBottom(50);
$options->templateId("report");
$options->AddTemplateParameter("number","12345");

$grabzIt->URLToPDF("http://www.spacex.com", $options);
$grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
$options = new \GrabzIt\GrabzItPDFOptions();
$options->setMarginTop(50);
$options->setMarginBottom(50);
$options->setTemplateId("report");
$options->AddTemplateParameter("number","12345");

$grabzIt->URLToPDF("http://www.spacex.com", $options);
$grabzIt->Save("http://www.example.com/handler.php");
grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")
options = GrabzItPDFOptions.GrabzItPDFOptions()
options.marginTop = 50
options.marginBottom = 50
options.templateId = "report"
options.AddTemplateParameter("number","12345")

grabzIt.URLToPDF("http://www.spacex.com", options)
grabzIt.Save("http://www.example.com/handler.py")
grabzIt = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")
options = GrabzIt::PDFOptions.new()
options.marginTop = 50
options.marginBottom = 50
options.templateId = "report"
options.add_template_parameter("number","12345")

grabzIt.url_to_pdf("http://www.spacex.com", options)
grabzItClient.save("http://www.example.com/handler/index")

页眉或页脚未显示在PDF中

生成PDF文档时,必须确保有足够的空间来显示页眉或页脚。 为此,请指定足够大的顶部空白以显示页眉,并指定足够大的底部空白以显示页脚。 要获得正确的边距大小,可能需要反复尝试。