OiO.lk Blog pdf replace text in pdf file with node.js
pdf

replace text in pdf file with node.js


It is necessary to make a simple web form where will be downloaded pdf file, different they can be with pictures with etc. I want it to change (in place of one word was another) these words can be scattered in different parts of the pdf file, and this pdf without changes could be downloaded back after editing.Already tried through different libraries and I can write text but delete the old and in its place a new text, the functionality of libraries does not allow, now I’m trying through hummus here is the code of html page

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF Upload</title>
</head>
<body>
    <h1>PDF Upload</h1>
    <form id="uploadForm" enctype="multipart/form-data">
        <input type="file" name="pdfFile" accept=".pdf" required>
        <button type="submit">Upload PDF</button>
    </form>

    <script>
        document.getElementById('uploadForm').addEventListener('submit', async (event) => {
            event.preventDefault();

            const formData = new FormData(event.target);

            try {
                const response = await fetch('/upload-pdf', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) {
                    throw new Error('Error uploading file: ' + response.statusText);
                }

                const result = await response.json();
                alert(result.message);
            } catch (error) {
                console.error('Error:', error);
                alert('Error uploading file: ' + error.message);
            }
        });
    </script>
</body>
</html>

and the server code

const app = express();
const port = 3000;
const path = require('path'); // Importing the path module
const hummus = require('hummus'); // Importing HummusJS

// Route for displaying the HTML page
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html')); // Sending the HTML file on GET request to the root route
});

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Working with PDF
const fs = require('fs');
const { PDFDocument, rgb } = require('pdf-lib'); // Importing the pdf-lib library
const pdfParse = require('pdf-parse');

const multer = require('multer');
// PDF Upload

// Setting up Multer for file uploads
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/'); // Ensure this folder exists
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname); // Save the file with its original name
    }
});

const upload = multer({ storage: storage });

async function modifyExistingPDF(sourceFilePath, findText, replaceText) {
    // Check if the source file exists
    if (!fs.existsSync(sourceFilePath)) {
        throw new Error(`Source file ${sourceFilePath} does not exist.`);
    }

    // Create a writer for modifying the PDF
    const modPdfWriter = hummus.createWriterToModify(sourceFilePath, { modifiedFilePath: './output.pdf', compress: false });

    // Get the copying context
    const sourceParser = modPdfWriter.createPDFCopyingContextForModifiedFile().getSourceDocumentParser();

    // Read the first page
    const pageObject = sourceParser.parsePage(0);
    
    // Get the contents stream
    const contentsStream = sourceParser.queryDictionaryObject(pageObject.getDictionary(), 'Contents');

    // Check for the existence of the contents stream
    if (!contentsStream) {
        throw new Error('Contents stream not found');
    }

    // Read the original block of text data
    const data = [];
    const readStream = sourceParser.startReadingFromStream(contentsStream);
    
    while (readStream.notEnded()) {
        const readData = readStream.read(10000);
        data.push(...readData);
    }

    // Create a new string
    let string = Buffer.from(data).toString('utf-8');

    // Log the original string
    console.log('Original String:', string);

    // Replace the text
    if (string.includes(findText)) {
        string = string.replace(new RegExp(findText, 'g'), replaceText);
    } else {
        throw new Error(`Text "${findText}" not found in PDF.`);
    }

    // Create and write the new text object
    const objectsContext = modPdfWriter.getObjectsContext();
    const textObjectID = pageObject.getDictionary().toJSObject().Contents.getObjectID();

    // Check if this is indeed the object we want to modify
    if (typeof textObjectID !== 'number') {
        throw new Error('Invalid text object ID.');
    }

    objectsContext.startModifiedIndirectObject(textObjectID);

    // Create a stream and write the data
    const stream = objectsContext.startUnfilteredPDFStream();
    stream.getWriteStream().write(Buffer.from(string, 'utf-8'));
    objectsContext.endPDFStream(stream);
    objectsContext.endIndirectObject();

    // Finish writing the PDF
    modPdfWriter.end();

    // Remove old objects that are no longer in use
    hummus.recrypt('./output.pdf', './outputClean.pdf');
}

app.post('/upload-pdf', upload.single('pdfFile'), async (req, res) => {
    console.log('File is being uploaded to the server...');

    if (!req.file) {
        return res.status(400).json({ error: 'File not uploaded' });
    }

    const sourceFilePath = req.file.path; // Path to the uploaded file
    const findText="Word"; // Text to search for (you can change this)
    const replaceText="Letter"; // Text to replace with

    try {
        await modifyExistingPDF(sourceFilePath, findText, replaceText); // Processing the PDF
        res.json({ message: 'PDF processed successfully!' });
    } catch (error) {
        console.error('Error processing PDF:', error);
        res.status(500).json({ error: 'Error processing PDF' });
    }
});

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

// Starting the server
app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});

………………………….



You need to sign in to view this answers

Exit mobile version