0

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;
        }));
Dyd666
  • 705
  • 1
  • 11
  • 30
  • What part of the filename are you trying to get? I assume you are trying to return part of it to provide a directory for that file - that is what a function does in gulp.dest. Look at the 'path' plugin https://nodejs.org/api/path.html#path_path_basename_path_ext it makes these thing much easier to work with. For instance, path.basename(fileName, '.pdf' may be what you are looking for but I am not sure exactly what you are trying to accomplish. A little more info would help. – Mark Oct 20 '17 at 01:59
  • path.basename(file.path, '.pdf') to be correct - you don't the substr stuff. You do need to var path = require('path') for that to work. – Mark Oct 20 '17 at 02:17
  • Hey @Mark! what i need it's providing the file path to the pdf method, which will be used by the gulp-html-pdf module as a title page for the generated pdf. Thanks anyway for the substr tip ;) – Dyd666 Oct 20 '17 at 07:03

1 Answers1

1

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'));
});
John
  • 1,313
  • 9
  • 21
  • 1
    Hi! No, no need to rename files. I just want to get the path of the processed file and give it in input to the `pdf` method ;) – Dyd666 Oct 20 '17 at 07:06
  • Sorry, seen too late your edited answer. I'll give it a try, thanks! – Dyd666 Oct 20 '17 at 10:27
  • Great @Dyd666. I have noticed that you don't need the return in the gulp-rename function. I will edit my answer to clarify. – John Oct 21 '17 at 15:24