diff --git a/Document-Processing-toc.html b/Document-Processing-toc.html index 6208fdd15..653bf68cb 100644 --- a/Document-Processing-toc.html +++ b/Document-Processing-toc.html @@ -3223,6 +3223,7 @@
  • Vue
  • JavaScript
  • TypeScript
  • +
  • Node.js
  • Loading and Saving
  • diff --git a/Document-Processing/PDF/PDF-Library/javascript/Create-PDF-document-node.md b/Document-Processing/PDF/PDF-Library/javascript/Create-PDF-document-node.md new file mode 100644 index 000000000..a9db5b3e9 --- /dev/null +++ b/Document-Processing/PDF/PDF-Library/javascript/Create-PDF-document-node.md @@ -0,0 +1,156 @@ +--- +layout: post +title: Create or Generate PDF file in Node | Syncfusion +description: Learn how to create a PDF file in Node.js server with easy steps using Syncfusion TypeScript PDF library without depending on Adobe +platform: document-processing +control: PDF +documentation: ug +keywords: pdf, script, express, node +--- + +# Create or Generate a PDF File in a Node.js Server Environment + +The Syncfusion® TypeScript PDF library is used to create, read, and edit PDF documents. This library also offers functionality to merge, split, stamp, fill forms, and secure PDF files. + +This guide explains how to integrate the Syncfusion TypeScript PDF library into an application with **Node.js server environment**. + +## Installing the Syncfusion TypeScript PDF package + +All Syncfusion JS 2 packages are published in `npmjs.com` registry. + +* To install the PDF library, use the following command. + +```bash +npm install @syncfusion/ej2-pdf --save +``` + +N> For data extraction features, you need to install the `@syncfusion/ej2-pdf-data-extract` package as an add-on. +Image extraction and image‑based redaction features are optimized for browser environments where visual rendering is available. These features rely on browser technologies such as DOM APIs, Canvas, and client‑side rendering, and therefore are not supported in pure Node.js server environments. + +## Dependencies + +The following list of dependencies are required to use the `TypeScript PDF library` component in your application. + +```bash +|-- @syncfusion/ej2-compression +|-- @syncfusion/ej2-base +``` + +## Create a Node.js Server Application to Generate a PDF Document + +* Add a simple button to `index.html` and attach a click handler that uses the TypeScript PDF API to create a new PDF document. + +{% tabs %} +{% highlight html tabtitle="index.html" %} + + + PDF Generator + + +

    Generate PDF Example

    + + + + +{% endhighlight %} +{% endtabs %} + +* Create a new project folder and initialize it. + +```bash +mkdir pdf-node-app +cd pdf-node-app +npm init -y +``` + +* Include the following namespaces in `server.js` file. + +{% tabs %} +{% highlight typescript tabtitle="index.ts" %} + +import { PdfDocument, PdfGraphics, PdfPage, PdfFont, PdfFontFamily, PdfFontStyle, PdfBrush } from '@syncfusion/ej2-pdf'; + +{% endhighlight %} +{% endtabs %} + +* Include the following code example in the Node.js environment to generate a PDF document + +{% tabs %} +{% highlight typescript tabtitle="server.js" %} + +const express = require('express'); +const { + PdfDocument, + PdfFontFamily, + PdfFontStyle, + PdfBrush +} = require('@syncfusion/ej2-pdf'); +const app = express(); +const PORT = 3000; +// Serve frontend files +app.use(express.static('public')); +// PDF generation API +app.get('/generate-pdf', (req, res) => { + // Create PDF document + const document = new PdfDocument(); + const page = document.addPage(); + const graphics = page.graphics; + const font = document.embedFont( + PdfFontFamily.helvetica, + 36, + PdfFontStyle.regular + ); + const brush = new PdfBrush({ r: 0, g: 0, b: 0 }); + graphics.drawString( + 'Hello from Frontend!', + font, + { + x: 20, + y: 20, + width: graphics.clientSize.width - 20, + height: 60 + }, + brush + ); + // Save PDF as bytes + const pdfBytes = document.save(); + document.destroy(); + // Send PDF to browser + res.setHeader('Content-Type', 'application/pdf'); + res.setHeader('Content-Disposition', 'attachment; filename=Output.pdf'); + res.send(Buffer.from(pdfBytes)); +}); +// Start server +app.listen(PORT, () => { + console.log(`Server running at http://localhost:${PORT}`); +}); + +{% endhighlight %} +{% endtabs %} + +* **Run the application** + +Run the following command to start the Node.js server: + +```bash +node server.js +``` + +This command starts the server and allows you to generate and download the PDF file from the browser. + +By executing the program, you will get the PDF document as follows. + +![Output PDF document](Getting_started_images/Output.png)