Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Image rendering doesn't work with relative paths using ../ #70

Closed
fgalan opened this issue Mar 26, 2020 · 7 comments
Closed

bug: Image rendering doesn't work with relative paths using ../ #70

fgalan opened this issue Mar 26, 2020 · 7 comments
Labels

Comments

@fgalan
Copy link

fgalan commented Mar 26, 2020

Context:

  • version (md-to-pdf -v): 2.8.2
  • platform (Unix, macOS, Windows): Debian Linux
  • node version: 8.17.0

Describe the bug:

As far as I checked md-to-pdf is able to render images in resulting PDF correctly when the image in the markdown file doesn't use ../. For instance if I have:

<img src="doc_images/chart/table.png"> 

It works.

However, if I have a relative path using ../ it doesn't work and I get the usual "broker image" thumb in the PDF:

imagen

For instance:

<img src="../doc_images/charts/table.png">

This is how I call md-to-pdf (pathnames are not the actual ones, for the sake of simplicity, the important thing here is that I'm not using any parameter)

md-to-pdf /path/to/file.md document.pdf

It is possible to use relative path with ../? What I'm doing wrong in that case?

(Sorry if this is not a bug but a known limitation or a bad usage on my side... I have looked at the documentation at https://github.com/simonhaenisch/md-to-pdf/blob/master/readme.md but I haven't found anything about this)

@fgalan fgalan added the bug label Mar 26, 2020
@simonhaenisch simonhaenisch removed the bug label Mar 26, 2020
@simonhaenisch
Copy link
Owner

simonhaenisch commented Mar 26, 2020

Hi, you should be able to work around this with the --basedir option:

The current working directory (process.cwd()) serves as the base directory of the file server by default. This can be adjusted with the --basedir flag (or equivalent config option).

Let's say your file is /var/my-project/docs/file.md and your image is /var/my-project/doc_images/picture.png, then you'd have to use /var/my-project as the base dir, and then the ../ part of the image path (../doc_images/picture.png) should be able to be resolved correctly.

md-to-pdf /var/my-project/docs/file.md --basedir /var/my-project

(relative paths should work fine as well)

Can you give it a try please and let me know if it doesn't work?

@fgalan
Copy link
Author

fgalan commented Mar 27, 2020

Thanks for the feedback! I'll try to use the --basedir option.

However, looking to the available options it doesn't appear:

$ md-to-pdf --help

  $ md-to-pdf [options] [path/to/file.md] [path/to/output.pdf]

  Options:

    -h, --help ............... Output usage information
    -v, --version ............ Output version
    -w, --watch .............. Watch the current file(s) for changes
    --stylesheet ............. Path to a local or remote stylesheet (can be passed multiple times)
    --css .................... String of styles
    --body-class ............. Classes to be added to the body tag (can be passed multiple times)
    --highlight-style ........ Style to be used by highlight.js (default: github)
    --marked-options ......... Set custom options for marked (as a JSON string)
    --pdf-options ............ Set custom options for the generated PDF (as a JSON string)
    --launch-options ......... Set custom launch options for Puppeteer
    --md-file-encoding ....... Set the file encoding for the markdown file
    --stylesheet-encoding .... Set the file encoding for the stylesheet
    --as-html ................ Output as HTML instead
    --config-file ............ Path to a JSON or JS configuration file
    --devtools ............... Open the browser with devtools instead of creating PDF
    --debug .................. Show more output on errors

Maybe I'm using an old version and I should upgrade? In which version --basedir was implemented?

Thanks!

@simonhaenisch
Copy link
Owner

simonhaenisch commented Mar 27, 2020

Yup, you'll need to update to version 3: npm i -g md-to-pdf@latest.

Edit: the fix to make these kinds of paths work has also been added in version 3, I doubt it's possible at all with version 2.

@fgalan
Copy link
Author

fgalan commented Mar 27, 2020

Thanks for the feedback!

I have upgraded my setup to md-to-pdf 3.0.0 with node v10. Now I have:

$ node --version
v10.0.0
$ npm --version
5.6.0
$ md-to-pdf --version
3.0.0

However, I get an ugly error when I try to use md-to-pdf, even in the simple case (without --basedir):

+ md-to-pdf /home/fermin/src/project/docs/component/chart.md /tmp/tmp.oCTOYwhj2j/zip/component/chart.pdf
  ✖ generating PDF from /home/fermin/src/project/docs/component/chart.md
    → Cannot read property 'readFile' of undefined
  ✖ generating PDF from /tmp/tmp.oCTOYwhj2j/zip/component/chart.pdf
    → Cannot read property 'readFile' of undefined
{ ListrError: Something went wrong
    at tasks.then (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/node_modules/listr/index.js:102:18)
    at process._tickCallback (internal/process/next_tick.js:178:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:721:11)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)
  name: 'ListrError',
  errors: 
   [ TypeError: Cannot read property 'readFile' of undefined
    at Object.exports.readFile (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/dist/lib/read-file.js:13:97)
    at Object.exports.convertMdToPdf (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/dist/lib/md-to-pdf.js:17:29)
    at Task.task (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/dist/cli.js:106:33)
    at Promise.resolve.then.then.skipped (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/node_modules/listr/lib/task.js:167:30)
    at process._tickCallback (internal/process/next_tick.js:178:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:721:11)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3),
     TypeError: Cannot read property 'readFile' of undefined
    at Object.exports.readFile (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/dist/lib/read-file.js:13:97)
    at Object.exports.convertMdToPdf (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/dist/lib/md-to-pdf.js:17:29)
    at Task.task (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/dist/cli.js:106:33)
    at Promise.resolve.then.then.skipped (/home/fermin/.nvm/versions/node/v10.0.0/lib/node_modules/md-to-pdf/node_modules/listr/lib/task.js:167:30)
    at process._tickCallback (internal/process/next_tick.js:178:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:721:11)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3) ],
  context: {} }

I have checked the input file exists:

$ ls /home/fermin/src/project/docs/component/chart.md -l
-rw-r--r-- 1 fermin fermin 2625 ene 28 12:51 /home/fermin/src/project/docs/component/chart.md

The placeholder for the output file also exists:

$ ls /tmp/tmp.oCTOYwhj2j -ld
drwx------ 3 fermin fermin 4096 mar 27 19:58 /tmp/tmp.oCTOYwhj2j

Maybe I'm doing something wrong?

@simonhaenisch
Copy link
Owner

Sorry, that's because Node 10.0.0 is too low, you need at least 10.17 (or at least 11.14), because that's the minimum version to provide the fs.promises API. I need to update the engines field so that a warning is thrown. However this is not as simple as setting it to >=10.17 because e. g. Node 11.0 is higher than 10.17 but still doesn't work (it was backported from 11.14 into 10.17).

@simonhaenisch
Copy link
Owner

I figured out how to set the engine field correctly and published a new version (3.0.1).

I'll close this for now but if you're still having problems with relative paths, please feel free to re-open this issue! Thanks.

@fgalan
Copy link
Author

fgalan commented Mar 30, 2020

After installing node 10.17.0 and getting familiar with the CLI of the 3.0.0 version, I finally solved the problem and now I'm able to render my documents without broken images. Thanks!

Some additional feedback, in the case you want to take it into account:

simonhaenisch added a commit that referenced this issue Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants