Question:
How to generate PDF with vfs_fonts [vuejs 3]

Problem

I'm studying Vuejs 3 with PDF generation.


I could generate a PDF file. But when I try to add the Japanese font function doesn't work.


I generate 'vfs_fonts' successfully using the below step.

>https://pdfmake.github.io/docs/0.1/fonts/custom-fonts-client-side/vfs/


I tried these two packages 'pdfmake' and 'vue3-pdfmake' because once I add 'vfs_fonts' PDF function stops.


I use vuejs 3 and composition API. Could you teach me what is a problem, please?


App.js


import { PDFPlugin } from 'vue3-pdfmake';


createApp({

    components: {

        CompaniesIndex,

        PDFPlugin

    }

}).use(router).use(PDFPlugin).mount('#app');


CompaniesPdf.vue


<template>

  <button @click="onGenPDF">Gen PDF</button>

</template>


<script setup>

import { usePDF } from 'vue3-pdfmake';

//import pdfMake from "pdfmake/build/pdfmake";

//import { usePDF } from "pdfmake/build/pdfmake";


import pdfFonts from "pdfmake/build/vfs_fonts";


pdfMake.vfs = pdfFonts.pdfMake.vfs;


pdfMake.fonts = {

  Ipaex: {

        normal: 'ipaexg.ttf',

        bold:   'ipaexm.ttf'

    }

}

const defaultStyle = "Ipaex"


const pdfmake = usePDF({

  autoInstallVFS: true

})


const onGenPDF = () => {

  pdfmake.createPdf({

    content: [

      '春夏秋冬 means four season in Japanese ',

    ],

    defaultStyle: {

            font:defaultStyle

    }

  }).download();

}

</script>


Error Message

import pdfFonts from "pdfmake/build/vfs_fonts"; ^ error during build: Error: 'default' is not exported by >node_modules/pdfmake/build/vfs_fonts.js, imported by resources/js/components/companies/CompaniesPdf.vue

 

Solution

I just solved this myself. I will post my current code. Thank you very much for all checking my question.


<template>

  <button @click="exportPdf">Gen PDF</button>

</template>


<script setup>

import * as pdfFonts from  'pdfmake/build/vfs_fonts';

import pdfMake from "pdfmake";

pdfMake.vfs = pdfFonts.pdfMake.vfs


pdfMake.fonts = {

  Ipaex: {

      normal: 'ipaexg.ttf',

      bold:   'ipaexm.ttf'

    }

}


const defaultStyle = "Ipaex"


function exportPdf() {

    let text = [["four_season","春夏秋冬"]]

    let document = {

        content: [

            {

                table: {

                    widths: [100, 100],

                    body: text

                }

            },

        ],

        defaultStyle: {

            font:defaultStyle

        }

    }   

    pdfMake.createPdf(document).open();

}

</script>


Suggested blog

>Create a project using Vue.js: Beginners Guide

>Authentication with Vue 3 and Firebase

>Plugins and Presets for Vuejs project

>Create a Vue.js application with CLI Services

>Create a project using Vue.js: Beginners Guide

>Create Vue.js application API


Nisha Patel

Nisha Patel

Submit
0 Answers