Search code examples
javascriptgulphtml-pdf

Pass params through pipes


Scenario: i'm iterating through several .md files, converting them first in HTML and then PDF. I need the file path on one of my pipe (the pdf one) but i don't know how to do it.

EDIT:

  1. I do not need to rename the files as they are piped through.
  2. I want to get the file path of the processed file on the pdf pipe, since i want to use it in that pipe (as shown in the code).
var pdf= require('gulp-html-pdf');
return gulp.src(path.join(config.ROOT, '**/docs/**/*.md'))

        // doing stuff...

        .pipe(gulp.dest(function(file) {
            return file.base;
        }))
        // Converting it to PDF
        .pipe(
             // i need the file path here, in order to use it for the PDF options
             pdf({
            "format": "A4",
            "border": {
                "top": "0cm",      // default is 0, units: mm, cm, in, px
                "right": "2.5cm",
                "bottom": "0cm",
                "left": "2.5cm"
            },
            "header": {
                "height": "3cm",
                "contents": '<header style="text-align: center;">' + FILE PATH + '</header>'
            },
            "footer": {
                "height": "3cm",
                "contents": '<footer style="height:3cm; text-align:right; padding-top:1cm">Page: {{page}}</span>/<span>{{pages}}</footer>'
            },
            "phantomArgs": []
        }))
        // Saving PDF
        .pipe(gulp.dest(function(file) {
            var fileName = file.path.substr(file.path.lastIndexOf('docs\\') + 5);
            console.log("...creating ", fileName);
            return file.base;
        }));

Solution

  • From your code, it looks like you are attempting to rename the files as they are piped through from HTML to pdf.

    If this is the case then the gulp-rename package is almost certainly what you are after.

    EDIT

    The gulp-html-pdf does not support file-by-file options so it is not possible to use it directly.

    You can roll your own solution using gulp-each and the html-pdf package which is used in gulp-html-pdf under the hood. Something like this should hopefully get you there:

    var gulp = require('gulp')
    var each = require('gulp-each')
    var pdf = require('html-pdf')
    var rename = require('gulp-rename')
    
    gulp.task('toPDF', function() {
       return gulp.src('*.html')
           .pipe(each(function(content, file, callback) {
            // path name is:
            var path = file.path
            // create pdf with options and convert back to buffer
            pdf.create(content, {/* file by file options*/})
                .toBuffer(function (err,buffer){
                    if(err){
                        //pass error back
                        callback(err, null)
                    }
               // return the buffer     
               callback(null, buffer);
            })
           }))
           // change file extention
           .pipe(rename( function (path) { path.extname = '.pdf'}))
           .pipe(gulp.dest('output'));
    });