PDFMAKE:仅在 GULP 之后在虚拟文件系统中找不到“Roboto-Regular.ttf”

新手上路,请多包涵

我使用 knockout/bootstrap/gulp 创建了一个简单的应用程序,它使用 pdfMake.js 下载 pdf。它在使用 VS2017 的调试模式下工作正常。发布并使用 gulp 后,运行时出现此错误: 在虚拟文件系统中找不到文件 ‘Roboto-Regular.ttf’

注意:gulp 之后,所有的 JS 文件都在一个 script.js 文件中。

我尝试了很多东西,它在调试时总是有效,只要我运行 gulp,它就会给出错误。

我从 这里 尝试了 joepal1976 的解决方案(我对 require.config.js 中的依赖项做了什么)

有人建议 .pipe(uglify({ compress: { hoist_funs: false } })) 似乎没有帮助。

像这样包含在 require.config 中:

 var require = {
    baseUrl: ".",
    paths: {
        "jquery":               "js-libs/jquery.min",
        "bootstrap":            "js-libs/bootstrap.min",
        "crossroads":           "js-libs/crossroads.min",
        "hasher":               "js-libs/hasher.min",
        "knockout":             "js-libs/knockout",
        "knockout-projections": "js-libs/knockout-projections.min",
        "signals":              "js-libs/signals.min",
        "text":                 "js-libs/text",
        "vfs_fonts":            "js-libs/vfs_fonts",
        "pdfMake":              "js-libs/pdfmake.min"
    },
    shim: {
        "bootstrap": { deps: ["jquery"] },
        'pdfMake':
        {
            exports: 'vfs_fonts'
        },
        'vfs_fonts':
        {
            deps: ['pdfMake'],
            exports: 'vfs_fonts'
        }
    }
};

页面的JS:

 define(["knockout", "text!./home.html"], function (ko, homeTemplate) {
function HomeViewModel(route) {
    var thisVM = this;

    this.VMInit = function () {

        var thePDF = {
            content: [
                'My test invoice.',
            ]
        };

        pdfMake.createPdf(thePDF).download('pdf_test.pdf');

    }

    thisVM.VMInit();
}
return { viewModel: HomeViewModel, template: homeTemplate };

});

吞咽文件:

 //-----------------------------------------------------------------------
// Node modules
var fs      = require('fs'),
vm      = require('vm'),
merge   = require('deeply'),
chalk   = require('chalk'),
es      = require('event-stream');

//-----------------------------------------------------------------------
// Gulp and plugins
var gulp        = require('gulp'),
rjs         = require('gulp-requirejs-bundler'),
concat      = require('gulp-concat'),
clean       = require('gulp-clean'),
replace     = require('gulp-replace'),
uglify      = require('gulp-uglify'),
htmlreplace = require('gulp-html-replace');

// Config
var requireJsRuntimeConfig =
vm.runInNewContext(fs.readFileSync('src/app/require.config.js') + '; require;');
requireJsOptimizerConfig = merge(requireJsRuntimeConfig, {
    out: 'scripts.js',
    baseUrl: './src',
    name: 'app/startup',
    paths: {
        requireLib: 'js-libs/require'
    },
    include: [
        'requireLib',
        'components/nav-bar/nav-bar',
        'components/home-page/home',
        'text!components/about-page/about.html'
    ],
    insertRequire: ['app/startup'],
    bundles: {
        // If you want parts of the site to load on demand, remove them from the 'include' list
        // above, and group them into bundles here.
        // 'bundle-name': [ 'some/module', 'another/module' ],
        // 'another-bundle-name': [ 'yet-another-module' ]
    }
});

//-----------------------------------------------------------------------
// Discovers all AMD dependencies, concatenates together all required .js
files, minifies them
gulp.task('js', function () {
    return rjs(requireJsOptimizerConfig)
        .pipe(replace('Views/src/', ''))
        .pipe(replace('img/', 'Assets/img/'))
        .pipe(replace('css/', 'Assets/css/'))
        .pipe(uglify({
            preserveComments: 'some'
        }))
        .pipe(gulp.dest('./dist-app/Assets/js/'));
});

gulp.task('css', function () {
return gulp.src(['./src/css/bootstrap.css',
                 './src/css/bootstrap-switch.css',
                 './src/css/dataTables.bootstrap.css',
                 './src/css/dataTables.colVis.css',
                 './src/css/dataTables.responsive.css',
                 './src/css/daterangePicker.css'])
    .pipe(concat('styles.css'))
    .pipe(gulp.dest('./dist-app/Assets/css/'));
});

// Copies index.html, replacing <script> and <link> tags to reference production
URLs
gulp.task('html', function () {
return gulp.src('./src/index.html')
    .pipe(htmlreplace({
        dependencies_top: '<link href="Assets/css/styles.css"
rel="stylesheet">',
        dependencies_bottom: '<script src="Assets/js/scripts.js"></script>'
    }))
    .pipe(gulp.dest('./dist-app/'));
});

// Removes all files from ./dist/
gulp.task('clean', function () {
console.log("the clean task");
return gulp.src('./dist-app/**/*', { read: false })
    .pipe(clean());
});

// All tasks in [] must complete before 'default' can begin
gulp.task('default', ['html', 'js', 'css'], function (callback) {
callback();
console.log('\nPlaced optimized files in ' + chalk.magenta('dist-app/\n'));
});

Startup.js 文件(如果有帮助):

 define(['jquery',
    'knockout',
    './router',
    'bootstrap',
    'knockout-projections',
    'pdfMake',
    'vfs_fonts'], function ($, ko, router) {

// Components can be packaged as AMD modules, such as the following:
ko.components.register('nav-bar', { require: 'components/nav-bar/nav-bar' });
ko.components.register('home-page', { require: 'components/home-page/home'
});

// ... or for template-only components, you can just point to a .html file
directly:
ko.components.register('about-page', {
template: { require: 'text!components/about-page/about.html' }
});

ko.components.register('new-page', { require: 'components/new-page/new-page'
});

// [Scaffolded component registrations will be inserted here. To retain this
//feature, don't remove this comment.]

// Start the application
ko.applyBindings({ route: router.currentRoute });
});

原文由 esmc 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 295
1 个回答

以下代码对我有用:

 import pdfMake from "pdfmake/build/pdfmake";
import pdfFonts from "pdfmake/build/vfs_fonts";
pdfMake.vfs = pdfFonts.pdfMake.vfs;

原文由 Rijo 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题