Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions UsesCases/ChangeLayout/changeLayoutHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
import fs from 'node:fs/promises';
import path from 'node:path';
import { PdfApi } from "../../src/api/api.js";
import { Rotation } from "../../src/models/rotation.js";
import { DocumentConfig } from "../../src/models/documentConfig.js"
import { DocumentProperties } from "../../src/models/documentProperties.js"
import { DocumentProperty } from "../../src/models/documentProperty.js"
import { DisplayProperties } from "../../src/models/displayProperties.js"
import { DefaultPageConfig } from "../../src/models/defaultPageConfig.js"
import { ImageStamp } from "../../src/models/imageStamp.js"
import { HorizontalAlignment } from "../../src/models/horizontalAlignment.js"
import { VerticalAlignment } from "../../src/models/verticalAlignment.js"
import { HtmlDocumentType } from "../../src/models/htmlDocumentType.js";
import { OutputFormat } from "../../src/models/outputFormat.js"

export { configParams, pdfApi, PdfChangeLayoutHelper };

const configParams = {
LOCAL_FOLDER: "C:\\Samples\\",
PDF_DOCUMENT_NAME: "sample.pdf",
TEMP_FOLDER: 'TempPdfCloud',

ROTATE_ANGLE: Rotation.on90,
ROTATE_PAGES: "1-3",

CROP_PAGE_TEMP_FILE: "sammple_temp_file.png",
CROP_LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
CROP_PAGE_NUMBER: 3,
CROP_PAGE_WIDTH: 0,
CROP_PAGE_HEIGHT: 0,
CROP_HEIGHT: 400,
CROP_WIDTH: 300,
CROP_LLX: 100,
CROP_LLY: 200,

RESIZE_PDF_HTML_FILE: "sammple_temp_file.html",
RESIZE_RESULT_DOCUMENT_NAME:"output_sample.pdf",
RESIZE_PAGE_NUMBER: 2,
RESIZE_NEW_PAGE_WIDTH: 1000,
RESIZE_NEW_PAGE_HEIGHT: 3000,

};

const pdfApi = new PdfApi(credentials.id, credentials.key);

const PdfChangeLayoutHelper = {
async uploadFile (fileName, localFolder, tempFolder) {
const fileNamePath = path.join(localFolder, fileName);
const fileData = await fs.readFile(fileNamePath);
const storagePath = path.join(tempFolder, fileName);
await pdfApi.uploadFile(storagePath, fileData)
.then(() => console.log("File: '" + fileName +"' successfully uploaded."));
},

async uploadDocument(document, localFolder, tempFolder) {
await this.uploadFile(document, localFolder, tempFolder)
},

async downloadResult(document, localFolder, tempFolder, prefix) {
const fileName = path.join(tempFolder, document);
const changedPdfData = await pdfApi.downloadFile(fileName);
const filePath = path.join(localFolder, prefix + document);
await fs.writeFile(filePath, changedPdfData.body);
console.log("Downloaded: " + filePath);
},

async getPageInfo (document, pageNumber, tempFolder) {
const resultPages = await pdfApi.getPage( document, pageNumber, { folder: tempFolder } );

if (resultPages.body.code == 200 && resultPages.body.page) {
this.showPages( [ resultPages.body.page ], "page");
configParams.PAGE_HEIGHT = resultPages.body.page.rectangle.uRY - resultPages.body.page.rectangle.lLY;
configParams.PAGE_WIDTH = resultPages.body.page.rectangle.uRX - resultPages.body.page.rectangle.lLX;
return {
"width": configParams.PAGE_WIDTH,
"height": configParams.PAGE_HEIGHT
}
}
else {
console.error("Unexpected error : can't get pages!!!");
return null;
}
},

showPages (pages, prefix) {
if (Array.isArray(pages) && pages.length > 0)
{
pages.forEach(function(page) {
console.log(prefix +" => id: '" + page.id + "', lLx: '" + page.rectangle.lLX + "', lLY: '" + page.rectangle.lLY + "', uRX: '" + page.rectangle.uRX + "', uRY: '" + page.rectangle.uRY + "'");
});
}
else
console.error("showPages() error: array of pages is empty!")
},

async extractPdfPage(document, pageNumber, width, height, localFolder, tempFolder) {
const response = await pdfApi.getPageConvertToPng(document, pageNumber, Math.trunc(width), Math.trunc(height), tempFolder);
if (response.response.status != 200)
{
console.error("extractPdfPage(): Faild to convert page to image!");
return null;
}
const filePath = path.join(localFolder, document + ".png");
await fs.writeFile(filePath, response.body);

const imageFile = document + ".png";
const imagePath = localFolder;

await this.uploadFile(imageFile, localFolder, tempFolder);

console.log("Page #" + pageNumber + " extracted as image.");
return imageFile;
},

async createPdfDocument(document, width, height, tempFolder) {
const pdfConfig = new DocumentConfig();
pdfConfig.pagesCount = 1;

pdfConfig.displayProperties = new DisplayProperties();
pdfConfig.displayProperties.centerWindow = true;
pdfConfig.displayProperties.hideMenuBar = true;

pdfConfig.documentProperties = new DocumentProperties();
const docProperty = new DocumentProperty();
docProperty.builtIn = false;
docProperty.name = "prop1";
docProperty.value = "Val1";

pdfConfig.documentProperties.list = [ docProperty ];

pdfConfig.defaultPageConfig = new DefaultPageConfig();
pdfConfig.defaultPageConfig.height = height;
pdfConfig.defaultPageConfig.width = width;

const response = await pdfApi.postCreateDocument(document, pdfConfig, null, tempFolder);
console.log("Document #" + document + " created.")
return response;
},

async insertPageAsImage(document, imageFileValue, llx, lly, tempFolder) {
const stamp = new ImageStamp();
stamp.background = true;
stamp.horizontalAlignment = HorizontalAlignment.None;
stamp.verticalAlignment = VerticalAlignment.None;
stamp.opacity = 1;
stamp.rotate = Rotation.None;
stamp.rotateAngle = 0;
stamp.xIndent = -llx;
stamp.yIndent = -lly;
stamp.zoom = 1;
stamp.fileName = configParams.TEMP_FOLDER + '/' + imageFileValue;

const response = await pdfApi.postPageImageStamps(document, 1, [stamp], null, tempFolder);
console.log("Image iserted into '" + document + "document on page #1");
return response;
},
};
26 changes: 26 additions & 0 deletions UsesCases/ChangeLayout/changeLayoutLaunch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { configParams } from "./changeLayoutHelper.js";
import { PdfRotatePages } from "./rotatePageStdAngle.js"
import { PdfCropPage } from "./cropPage.js";
import { PdfResizePages } from "./resizeDocumentAllPages.js";

async function main() {
try {

await PdfRotatePages.rotate(configParams.PDF_DOCUMENT_NAME,
configParams.ROTATE_ANGLE, configParams.ROTATE_PAGES,
configParams.LOCAL_FOLDER, configParams.TEMP_FOLDER);

await PdfCropPage.cropPage(configParams.PDF_DOCUMENT_NAME,
configParams.CROP_PAGE_NUMBER, configParams.CROP_LLX, configParams.CROP_LLY, configParams.CROP_WIDTH, configParams.CROP_HEIGHT, configParams.CROP_LOCAL_RESULT_DOCUMENT_NAME,
configParams.LOCAL_FOLDER, configParams.TEMP_FOLDER);

await PdfResizePages.resizeAllPages(configParams.PDF_DOCUMENT_NAME,
configParams.RESIZE_PDF_HTML_FILE, configParams.RESIZE_NEW_PAGE_WIDTH, configParams.RESIZE_NEW_PAGE_HEIGHT, configParams.RESIZE_RESULT_DOCUMENT_NAME,
configParams.LOCAL_FOLDER, configParams.TEMP_FOLDER);

} catch (error) {
console.error("Error:", error.message);
}
}

await main();
30 changes: 30 additions & 0 deletions UsesCases/ChangeLayout/cropPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";

export { PdfCropPage };

const PdfCropPage = {
async cropPage(document, pageNumber, llx, lly, width, height, outputDocument, localFolder, tempFolder) {
if ( pdfApi ) {
await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder);

var pageSie = await PdfChangeLayoutHelper.getPageInfo(document, pageNumber, tempFolder);

const imageFile = await PdfChangeLayoutHelper.extractPdfPage(document, pageNumber, pageSie.width, pageSie.height, localFolder, tempFolder);
const newPdf = await PdfChangeLayoutHelper.createPdfDocument(outputDocument, width, height, tempFolder);
if (newPdf.body.code != 200) {
console.error("cropPage(): Failed to create new PDF document!");
return;
}

const response = await PdfChangeLayoutHelper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder);

if (response.body.code == 200) {
console.log("cropPage(): Page successfully cropped.");
await PdfChangeLayoutHelper.downloadResult(outputDocument, localFolder, tempFolder, "cropped_")
}
else
console.error("cropPage(): Can't crop pdf document page!")
}

},
};
50 changes: 50 additions & 0 deletions UsesCases/ChangeLayout/resizeDocumentAllPages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";
import path from 'node:path';
import { HtmlDocumentType } from "../../src/models/htmlDocumentType.js";
import { OutputFormat } from "../../src/models/outputFormat.js"

export { PdfResizePages }

const PdfResizePages = {
async resizeAllPages(document, htmlTempDoc, width, height, outputDocument, localFolder, tempFolder) {
await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder)

const htmlTempPath = path.join(tempFolder, htmlTempDoc);

const html_response = await pdfApi.putPdfInStorageToHtml(
document,
htmlTempPath,
null, null, null, null,
HtmlDocumentType.Xhtml,
null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
tempFolder,
null, null,
OutputFormat.Folder);

if (html_response.body.code != 200) {
console.error("resizePages(): Can't convert pdf to html!");
return;
}
else
console.log("resizePages(): temporary file '" + htmlTempDoc + "' succesfully creaated.")

const response = await pdfApi.putHtmlInStorageToPdf(
outputDocument,
htmlTempPath,
htmlTempDoc,
height,
width,
null, null, null, null, null,
tempFolder,
null);

if (response.body.code == 200) {
console.log("resizePages(): Pages successfully resized.");
await PdfChangeLayoutHelper.downloadResult(outputDocument, localFolder, tempFolder, "resized_doc_");
}
else
console.log("resizePages(): Can't convert html to pdf!")

},

};
22 changes: 22 additions & 0 deletions UsesCases/ChangeLayout/rotatePageStdAngle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";

export { PdfRotatePages };

const PdfRotatePages = {
async rotate(document, angle, pages, localFolder, tempFolder) {
if ( pdfApi) {
await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder);

const response = await pdfApi.postDocumentPagesRotate(
document, angle, pages, null, tempFolder);

if (response.body.code == 200) {
console.log("rotatePages(): Page successfully rotated.");
await PdfChangeLayoutHelper.downloadResult(document, localFolder, tempFolder, "rotated_output_");
}
else
console.error("rotatePages(): Can't rotate pdf document pages!")
}

},
};