shirshak55 / Rust-Book-In-PDF

Collection of Rust Books in PDF format.
https://github.com/shirshak55/Rust-Book-In-PDF/releases
MIT License
309 stars 18 forks source link

How to generate `.pdf` by yourself? #10

Closed Cloufish closed 1 year ago

Cloufish commented 1 year ago

I'm trying to convert Discovery STM F3 Embedded Book,

I added in config.toml

[Books.The_Rust_DiscoverySTM]
print_url="https://docs.rust-embedded.org/discovery/f3discovery/print.html"
file_name="f3discovery.pdf"

and then tried to execute /src/main.ts script: $ tsc /src/main.ts I generated main.js file,

and tried to execute it with: $ node /src/main.js

But I got an error:

file:///home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:38
exports.__esModule = true;
^

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/cloufish/Downloads/Rust-Book-In-PDF/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:38:1
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:526:24)
    at async loadESM (node:internal/process/esm_loader:91:5)

Something about a file with .js extension? I don't understand. I googling it I noticed it is a frequent error

The link -> https://stackoverflow.com/questions/70954671/typescript-nodejs-exports-is-not-defined-in-es-module-scope told that I should delete "type" : "module" line in package.json But it didn't fixed the issue and I got other errors:

Error TypeError: Cannot read properties of undefined (reading 'parse')
    at /home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:51:48
    at step (/home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:33:23)
    at Object.next (/home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:14:53)
    at /home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:4:12)
    at main (/home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:46:12)
    at Object.<anonymous> (/home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:129:1)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
/home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:131
    process_1["default"].exit(0);
                         ^

TypeError: Cannot read properties of undefined (reading 'exit')
    at /home/cloufish/Downloads/Rust-Book-In-PDF/src/main.js:131:26

Sorry If it is a silly question. But I need the help nonetheless!!!! xD

shirshak55 commented 1 year ago

Alright, let's fix this.

Firstly can you check your node version? Mine is

node -v
v19.5.0

And I ran the following command:

npm install

Then I ran the typescript development server in watch mode

npm run watch

After this, I went to a new tab in the terminal and issued the following command. I received the following output

npm run start
> node dist/main

Download from The_Rust_DiscoverySTM
Delay for 30 seconds
Successfully printed? /Users/q/Desktop/projects/Rust-Book-In-PDF/f3discovery.pdf
Completed

I am wondering if this is a node version difference. Let me know :).

Lastly, I have added this book to this repo config, so it will be available as soon as the PR is merged. If you would like to expedite the process you can also review it. https://github.com/shirshak55/Rust-Book-In-PDF/pull/11

Cloufish commented 1 year ago

XD Yes... my node version was 16.19.0... I don't usually self-host websites or build them xd. Sorry - it was silly issue indeed