Переменная печати внутри нижнего колонтитула создания PDF - PullRequest
0 голосов
/ 07 ноября 2018

Мне нужно напечатать поле объекта внутри нижнего колонтитула страницы. На данный момент у меня есть:

async function createPdf(obj, res) {
        //other code
        const pdf = await page.pdf({ 
        format: 'A4', 
            printBackground: true, 
            displayHeaderFooter : true,
            headerTemplate : "",
        footerTemplate : "<style>.footer{width: 100%; font-size:12px; text-align:right; color:white; background:#161719; -webkit-print-color-adjust:exact; margin-bottom:-15px;}</style><div class='footer'><span>page </span><span class='pageNumber'></span>/<span class='totalPages'></span></div>",
            //margin top 0 removes header
            margin: {top: "00", bottom: "18"}
        });
        //other code
}

Мне нужно напечатать какой-нибудь фильтр obj внутри нижнего колонтитула, но при всех способах, которые я пробовал, он печатает obj в виде строки, а не содержимого. Возможно ли достичь моей цели?

1 Ответ

0 голосов
/ 07 ноября 2018

Чтобы напечатать переменную внутри нижнего колонтитула при создании PDF с page.pdf(), вы можете использовать HTML-код footerTemplate внутри литерала шаблона (&#96;&#96;) и вставьте переменную внутри заполнителя (${}).

См. Пример ниже:

const example = 'Hello, world!';

const pdf = await page.pdf({
  format: 'A4',
  printBackground: true,
  displayHeaderFooter: true,
  headerTemplate: '',
  footerTemplate: `<style>
                   .footer {
                     background: #161719;
                     color: #fff;
                     -webkit-print-color-adjust: exact;
                     color-adjust: exact;
                     font-size: 12px;
                     margin-bottom: -15px;
                     text-align: right;
                     width: 100%;
                   }
                   </style>
                   <div class="footer">
                     <span>${example}</span> <!-- Variable Inside Footer -->
                     <span>page</span>
                     <span class="pageNumber"></span>/<span class="totalPages"></span>
                   </div>`,
  //margin top 0 removes header
  margin: {
    top: 0,
    bottom: 18,
  },
});
...