Skip to content

Commit 449af03

Browse files
committed
DOC-2582: Export to PDF with JWT authentication (Node.js) Guide.
1 parent fe5942f commit 449af03

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@
312312
**** xref:mediaembed-server-integration.adoc[Integrate Enhanced Media Embed Server]
313313
*** xref:advtable.adoc[Enhanced Tables]
314314
*** xref:exportpdf.adoc[Export to PDF]
315+
**** xref:export-to-pdf-with-jwt-authentication-nodejs.adoc[Export to PDF with JWT authentication (Nodejs)]
315316
**** xref:html-to-pdf-converter-api.adoc[HTML to PDF Converter API]
316317
*** xref:exportword.adoc[Export to Word]
317318
**** xref:html-to-docx-converter-api.adoc[HTML to DOCX Converter API]
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
= Export to PDF with JWT authentication (Node.js) Guide
2+
:navtitle: JWT Authentication setup for Export to PDF
3+
:description: Guide on how to setup JWT Authentication for exporting PDF files with {productname}
4+
:keywords: jwt, authentication, exportpdf, pdf, node.js
5+
:pluginname: Export to PDF
6+
:plugincode: exportpdf
7+
8+
9+
== Introduction
10+
11+
{pluginname} requires setting up JSON Web Token (JWT) authentication to maintain control over file security. A JWT endpoint generates and provides authorization tokens that verify submitted content is sent by authorized users, preventing unauthorized access. As a standard web services authorization solution, JWT is documented extensively at link:https://jwt.io/[https://jwt.io/].
12+
13+
This guide provides a comprehensive walkthrough for integrating {pluginname} with {productname}, including {pluginname} functionality, by using a Node.js server for JWT token generation. It covers project setup, server configuration, and {productname} customization.
14+
15+
== What You'll Build
16+
17+
Before diving into the technical details, here's what you'll achieve with this guide:
18+
19+
* A working PDF export system for your TinyMCE editor
20+
* A secure authentication system using JWT tokens
21+
* A simple Node.js server to handle the authentication
22+
23+
[TIP]
24+
====
25+
This guide is designed for developers new to JWT authentication and TinyMCE integration.
26+
====
27+
28+
== Prerequisites
29+
30+
Before starting, ensure you have:
31+
32+
* Node.js installed on your computer
33+
* A TinyMCE API key (get one from link:https://www.tiny.cloud/signup[TinyMCE's website])
34+
* Basic familiarity with the command line
35+
36+
[IMPORTANT]
37+
====
38+
Make sure you have your API key ready before starting. You'll need it for both the server and client configuration.
39+
====
40+
41+
== Quick Start Guide
42+
43+
=== Project Setup (5 minutes)
44+
45+
[source,bash]
46+
----
47+
# Create and enter project directory
48+
mkdir tinymce-pdf-export
49+
cd tinymce-pdf-export
50+
51+
# Initialize project
52+
npm init -y
53+
54+
# Install required packages
55+
npm install express cors jsonwebtoken
56+
----
57+
58+
Verify that the `package.json` file includes the below required dependencies:
59+
60+
[source,json]
61+
----
62+
{
63+
"dependencies": {
64+
"cors": "^2.8.5",
65+
"express": "^4.21.0",
66+
"jsonwebtoken": "^9.0.2"
67+
}
68+
}
69+
----
70+
71+
=== Create Project Structure
72+
73+
[source,bash]
74+
----
75+
# Create the public folder for your web files
76+
mkdir public
77+
touch public/index.html
78+
touch jwt.js
79+
----
80+
81+
Your project should look like this:
82+
83+
[source]
84+
----
85+
/tinymce-pdf-export
86+
/public
87+
index.html (TinyMCE webpage)
88+
jwt.js (Server code)
89+
package.json (Project configuration)
90+
----
91+
92+
. Inside the `public` folder where you created the `index.html` file add the HTML setup code (refer to the *Setting up {productname} HTML* section).
93+
94+
==== Web Page Setup (public/index.html)
95+
96+
[source,html]
97+
----
98+
<!DOCTYPE html>
99+
<html>
100+
<head>
101+
<title>TinyMCE with PDF Export</title>
102+
<script
103+
src="https://cdn.tiny.cloud/1/YOUR-API-KEY/tinymce/7/tinymce.min.js"
104+
referrerpolicy="origin">
105+
</script>
106+
<script>
107+
tinymce.init({
108+
selector: 'textarea',
109+
plugins: 'exportpdf',
110+
toolbar: 'exportpdf',
111+
exportpdf_converter_options: {
112+
'format': 'Letter',
113+
'margin_top': '1in',
114+
'margin_right': '1in',
115+
'margin_bottom': '1in',
116+
'margin_left': '1in'
117+
},
118+
exportpdf_service_url: "<serviceURL>",
119+
120+
// export_token_provider fetches a token from the `/jwt` endpoint.
121+
export_token_provider: () => {
122+
return fetch('http://localhost:3000/jwt', {
123+
method: 'POST',
124+
headers: { 'Content-Type': 'application/json' },
125+
}).then(response => response.json());
126+
},
127+
});
128+
</script>
129+
</head>
130+
<body>
131+
<h1>TinyMCE Export to PDF Demo</h1>
132+
<textarea>
133+
Welcome to TinyMCE! Try the Export to PDF feature.
134+
</textarea>
135+
</body>
136+
</html>
137+
----
138+
139+
. In the root directory, copy and paste the server setup code into the `jwt.js` file (refer to the *Configuring the Node.js Server for JWT Token Generation* section).
140+
141+
==== Server Setup (jwt.js)
142+
143+
[source,javascript]
144+
----
145+
const express = require('express'); // Sets up the web server.
146+
const jwt = require('jsonwebtoken'); // Generates and signs JWTs.
147+
const cors = require('cors'); // Allows cross-origin requests.
148+
const path = require('path'); // Handles file paths.
149+
150+
const app = express();
151+
app.use(cors());
152+
153+
// Your private key (Replace this with your actual key)
154+
const privateKey = `
155+
-----BEGIN PRIVATE KEY-----
156+
{Your private PKCS8 key goes here}
157+
-----END PRIVATE KEY-----
158+
`;
159+
160+
app.use(express.static(path.join(__dirname, 'public')));
161+
162+
// JWT token generation endpoint
163+
app.post('/jwt', (req, res) => {
164+
const payload = {
165+
aud: 'YOUR-API-KEY-HERE', // Replace with your actual API key
166+
iat: Math.floor(Date.now() / 1000), // Issue timestamp
167+
exp: Math.floor(Date.now() / 1000) + (60 * 10) // Expiration time (10 minutes)
168+
};
169+
170+
try {
171+
// Tokens are signed with the RS256 algorithm using your private key
172+
const token = jwt.sign(payload, privateKey, { algorithm: 'RS256' });
173+
res.json({ token });
174+
} catch (error) {
175+
res.status(500).send('Failed to generate JWT token.');
176+
console.error(error.message);
177+
}
178+
});
179+
180+
const PORT = 3000;
181+
app.listen(PORT, () => {
182+
console.log(`Server running at http://localhost:${PORT}`);
183+
});
184+
----
185+
186+
=== Configuration Steps
187+
188+
==== 1. Add Your API Key
189+
190+
* Replace `YOUR-API-KEY` in both files with your actual {productname} API key
191+
* The API key should be the same in both the HTML script source and the JWT payload
192+
193+
==== 2. Add Your Private Key
194+
195+
* Replace the private key placeholder in `jwt.js` with your actual private key
196+
* Make sure it's in `PKCS8` format
197+
* Keep this key secure and never share it publicly
198+
199+
=== Running Your Project
200+
201+
. Start the server:
202+
+
203+
[source,bash]
204+
----
205+
node jwt.js
206+
----
207+
208+
. Open your browser to: `http://localhost:3000`
209+
. You should see:
210+
* The TinyMCE editor
211+
* An "Export PDF" button in the toolbar
212+
213+
=== Start Server
214+
215+
* Define the port and start the server using `app.listen`.

0 commit comments

Comments
 (0)