Lambda[nodejs]でエクセルファイル内の文字列を置換したときのメモ
const XlsxPopulate = require('xlsx-populate') const outputXlsx = os.tmpdir() + '/contract.xlsx' try { const workbook = await XlsxPopulate.fromFileAsync(`${appRoot}/contents/contract-template.xlsx`) const sheet = workbook.sheet(0) const assigns = { __name__: '山下 テスト' // エクセル内の__name__という文字列を置換 } sheet._rows.forEach(row => { row._cells.forEach(cell => { const key = cell.value() if (assigns[key]) cell.value(assigns[key]) }) }) await workbook.toFileAsync(outputXlsx) } catch (err) { console.log(err) throw '契約書の作成に失敗しました' }
で、できたエクセルをaws-lambda-libreofficeを使ってPDFに変換しようとしたんですが、日本語が文字化けする問題を解消できず…他の方法に変えました。 lambdaにカスタムフォントを追加したらいいと試したけど、ライブラリの問題なのかnode12だからなのかフォントの設定がうまく反映されず…。
const { convertTo, canBeConvertedToPDF } = require('@shelf/aws-lambda-libreoffice') const outputPdf = os.tmpdir() + '/contract.pdf' await convertTo('contract.xlsx', 'pdf')