Skip to content

Commit fecd52f

Browse files
authored
Merge pull request #133 from aspose-pdf-cloud/pdfcloud-5009-added-snippets-rotate-resize-xrop
PDFCLOUD-5009: added snippets rotate-resize-crop
2 parents f9f83d2 + 289f2e2 commit fecd52f

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
import { Rotation } from "../../src/models/rotation.js";
6+
import { DocumentConfig } from "../../src/models/documentConfig.js"
7+
import { DocumentProperties } from "../../src/models/documentProperties.js"
8+
import { DocumentProperty } from "../../src/models/documentProperty.js"
9+
import { DisplayProperties } from "../../src/models/displayProperties.js"
10+
import { DefaultPageConfig } from "../../src/models/defaultPageConfig.js"
11+
import { ImageStamp } from "../../src/models/imageStamp.js"
12+
import { HorizontalAlignment } from "../../src/models/horizontalAlignment.js"
13+
import { VerticalAlignment } from "../../src/models/verticalAlignment.js"
14+
import { HtmlDocumentType } from "../../src/models/htmlDocumentType.js";
15+
import { OutputFormat } from "../../src/models/outputFormat.js"
16+
17+
export { configParams, pdfApi, PdfChangeLayoutHelper };
18+
19+
const configParams = {
20+
LOCAL_FOLDER: "C:\\Samples\\",
21+
PDF_DOCUMENT_NAME: "sample.pdf",
22+
TEMP_FOLDER: 'TempPdfCloud',
23+
24+
ROTATE_ANGLE: Rotation.on90,
25+
ROTATE_PAGES: "1-3",
26+
27+
CROP_PAGE_TEMP_FILE: "sammple_temp_file.png",
28+
CROP_LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
29+
CROP_PAGE_NUMBER: 3,
30+
CROP_PAGE_WIDTH: 0,
31+
CROP_PAGE_HEIGHT: 0,
32+
CROP_HEIGHT: 400,
33+
CROP_WIDTH: 300,
34+
CROP_LLX: 100,
35+
CROP_LLY: 200,
36+
37+
RESIZE_PDF_HTML_FILE: "sammple_temp_file.html",
38+
RESIZE_RESULT_DOCUMENT_NAME:"output_sample.pdf",
39+
RESIZE_PAGE_NUMBER: 2,
40+
RESIZE_NEW_PAGE_WIDTH: 1000,
41+
RESIZE_NEW_PAGE_HEIGHT: 3000,
42+
43+
};
44+
45+
const pdfApi = new PdfApi(credentials.id, credentials.key);
46+
47+
const PdfChangeLayoutHelper = {
48+
async uploadFile (fileName, localFolder, tempFolder) {
49+
const fileNamePath = path.join(localFolder, fileName);
50+
const fileData = await fs.readFile(fileNamePath);
51+
const storagePath = path.join(tempFolder, fileName);
52+
await pdfApi.uploadFile(storagePath, fileData)
53+
.then(() => console.log("File: '" + fileName +"' successfully uploaded."));
54+
},
55+
56+
async uploadDocument(document, localFolder, tempFolder) {
57+
await this.uploadFile(document, localFolder, tempFolder)
58+
},
59+
60+
async downloadResult(document, localFolder, tempFolder, prefix) {
61+
const fileName = path.join(tempFolder, document);
62+
const changedPdfData = await pdfApi.downloadFile(fileName);
63+
const filePath = path.join(localFolder, prefix + document);
64+
await fs.writeFile(filePath, changedPdfData.body);
65+
console.log("Downloaded: " + filePath);
66+
},
67+
68+
async getPageInfo (document, pageNumber, tempFolder) {
69+
const resultPages = await pdfApi.getPage( document, pageNumber, { folder: tempFolder } );
70+
71+
if (resultPages.body.code == 200 && resultPages.body.page) {
72+
this.showPages( [ resultPages.body.page ], "page");
73+
configParams.PAGE_HEIGHT = resultPages.body.page.rectangle.uRY - resultPages.body.page.rectangle.lLY;
74+
configParams.PAGE_WIDTH = resultPages.body.page.rectangle.uRX - resultPages.body.page.rectangle.lLX;
75+
return {
76+
"width": configParams.PAGE_WIDTH,
77+
"height": configParams.PAGE_HEIGHT
78+
}
79+
}
80+
else {
81+
console.error("Unexpected error : can't get pages!!!");
82+
return null;
83+
}
84+
},
85+
86+
showPages (pages, prefix) {
87+
if (Array.isArray(pages) && pages.length > 0)
88+
{
89+
pages.forEach(function(page) {
90+
console.log(prefix +" => id: '" + page.id + "', lLx: '" + page.rectangle.lLX + "', lLY: '" + page.rectangle.lLY + "', uRX: '" + page.rectangle.uRX + "', uRY: '" + page.rectangle.uRY + "'");
91+
});
92+
}
93+
else
94+
console.error("showPages() error: array of pages is empty!")
95+
},
96+
97+
async extractPdfPage(document, pageNumber, width, height, localFolder, tempFolder) {
98+
const response = await pdfApi.getPageConvertToPng(document, pageNumber, Math.trunc(width), Math.trunc(height), tempFolder);
99+
if (response.response.status != 200)
100+
{
101+
console.error("extractPdfPage(): Faild to convert page to image!");
102+
return null;
103+
}
104+
const filePath = path.join(localFolder, document + ".png");
105+
await fs.writeFile(filePath, response.body);
106+
107+
const imageFile = document + ".png";
108+
const imagePath = localFolder;
109+
110+
await this.uploadFile(imageFile, localFolder, tempFolder);
111+
112+
console.log("Page #" + pageNumber + " extracted as image.");
113+
return imageFile;
114+
},
115+
116+
async createPdfDocument(document, width, height, tempFolder) {
117+
const pdfConfig = new DocumentConfig();
118+
pdfConfig.pagesCount = 1;
119+
120+
pdfConfig.displayProperties = new DisplayProperties();
121+
pdfConfig.displayProperties.centerWindow = true;
122+
pdfConfig.displayProperties.hideMenuBar = true;
123+
124+
pdfConfig.documentProperties = new DocumentProperties();
125+
const docProperty = new DocumentProperty();
126+
docProperty.builtIn = false;
127+
docProperty.name = "prop1";
128+
docProperty.value = "Val1";
129+
130+
pdfConfig.documentProperties.list = [ docProperty ];
131+
132+
pdfConfig.defaultPageConfig = new DefaultPageConfig();
133+
pdfConfig.defaultPageConfig.height = height;
134+
pdfConfig.defaultPageConfig.width = width;
135+
136+
const response = await pdfApi.postCreateDocument(document, pdfConfig, null, tempFolder);
137+
console.log("Document #" + document + " created.")
138+
return response;
139+
},
140+
141+
async insertPageAsImage(document, imageFileValue, llx, lly, tempFolder) {
142+
const stamp = new ImageStamp();
143+
stamp.background = true;
144+
stamp.horizontalAlignment = HorizontalAlignment.None;
145+
stamp.verticalAlignment = VerticalAlignment.None;
146+
stamp.opacity = 1;
147+
stamp.rotate = Rotation.None;
148+
stamp.rotateAngle = 0;
149+
stamp.xIndent = -llx;
150+
stamp.yIndent = -lly;
151+
stamp.zoom = 1;
152+
stamp.fileName = configParams.TEMP_FOLDER + '/' + imageFileValue;
153+
154+
const response = await pdfApi.postPageImageStamps(document, 1, [stamp], null, tempFolder);
155+
console.log("Image iserted into '" + document + "document on page #1");
156+
return response;
157+
},
158+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { configParams } from "./changeLayoutHelper.js";
2+
import { PdfRotatePages } from "./rotatePageStdAngle.js"
3+
import { PdfCropPage } from "./cropPage.js";
4+
import { PdfResizePages } from "./resizeDocumentAllPages.js";
5+
6+
async function main() {
7+
try {
8+
9+
await PdfRotatePages.rotate(configParams.PDF_DOCUMENT_NAME,
10+
configParams.ROTATE_ANGLE, configParams.ROTATE_PAGES,
11+
configParams.LOCAL_FOLDER, configParams.TEMP_FOLDER);
12+
13+
await PdfCropPage.cropPage(configParams.PDF_DOCUMENT_NAME,
14+
configParams.CROP_PAGE_NUMBER, configParams.CROP_LLX, configParams.CROP_LLY, configParams.CROP_WIDTH, configParams.CROP_HEIGHT, configParams.CROP_LOCAL_RESULT_DOCUMENT_NAME,
15+
configParams.LOCAL_FOLDER, configParams.TEMP_FOLDER);
16+
17+
await PdfResizePages.resizeAllPages(configParams.PDF_DOCUMENT_NAME,
18+
configParams.RESIZE_PDF_HTML_FILE, configParams.RESIZE_NEW_PAGE_WIDTH, configParams.RESIZE_NEW_PAGE_HEIGHT, configParams.RESIZE_RESULT_DOCUMENT_NAME,
19+
configParams.LOCAL_FOLDER, configParams.TEMP_FOLDER);
20+
21+
} catch (error) {
22+
console.error("Error:", error.message);
23+
}
24+
}
25+
26+
await main();

UsesCases/ChangeLayout/cropPage.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";
2+
3+
export { PdfCropPage };
4+
5+
const PdfCropPage = {
6+
async cropPage(document, pageNumber, llx, lly, width, height, outputDocument, localFolder, tempFolder) {
7+
if ( pdfApi ) {
8+
await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder);
9+
10+
var pageSie = await PdfChangeLayoutHelper.getPageInfo(document, pageNumber, tempFolder);
11+
12+
const imageFile = await PdfChangeLayoutHelper.extractPdfPage(document, pageNumber, pageSie.width, pageSie.height, localFolder, tempFolder);
13+
const newPdf = await PdfChangeLayoutHelper.createPdfDocument(outputDocument, width, height, tempFolder);
14+
if (newPdf.body.code != 200) {
15+
console.error("cropPage(): Failed to create new PDF document!");
16+
return;
17+
}
18+
19+
const response = await PdfChangeLayoutHelper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder);
20+
21+
if (response.body.code == 200) {
22+
console.log("cropPage(): Page successfully cropped.");
23+
await PdfChangeLayoutHelper.downloadResult(outputDocument, localFolder, tempFolder, "cropped_")
24+
}
25+
else
26+
console.error("cropPage(): Can't crop pdf document page!")
27+
}
28+
29+
},
30+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";
2+
import path from 'node:path';
3+
import { HtmlDocumentType } from "../../src/models/htmlDocumentType.js";
4+
import { OutputFormat } from "../../src/models/outputFormat.js"
5+
6+
export { PdfResizePages }
7+
8+
const PdfResizePages = {
9+
async resizeAllPages(document, htmlTempDoc, width, height, outputDocument, localFolder, tempFolder) {
10+
await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder)
11+
12+
const htmlTempPath = path.join(tempFolder, htmlTempDoc);
13+
14+
const html_response = await pdfApi.putPdfInStorageToHtml(
15+
document,
16+
htmlTempPath,
17+
null, null, null, null,
18+
HtmlDocumentType.Xhtml,
19+
null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
20+
tempFolder,
21+
null, null,
22+
OutputFormat.Folder);
23+
24+
if (html_response.body.code != 200) {
25+
console.error("resizePages(): Can't convert pdf to html!");
26+
return;
27+
}
28+
else
29+
console.log("resizePages(): temporary file '" + htmlTempDoc + "' succesfully creaated.")
30+
31+
const response = await pdfApi.putHtmlInStorageToPdf(
32+
outputDocument,
33+
htmlTempPath,
34+
htmlTempDoc,
35+
height,
36+
width,
37+
null, null, null, null, null,
38+
tempFolder,
39+
null);
40+
41+
if (response.body.code == 200) {
42+
console.log("resizePages(): Pages successfully resized.");
43+
await PdfChangeLayoutHelper.downloadResult(outputDocument, localFolder, tempFolder, "resized_doc_");
44+
}
45+
else
46+
console.log("resizePages(): Can't convert html to pdf!")
47+
48+
},
49+
50+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";
2+
3+
export { PdfRotatePages };
4+
5+
const PdfRotatePages = {
6+
async rotate(document, angle, pages, localFolder, tempFolder) {
7+
if ( pdfApi) {
8+
await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder);
9+
10+
const response = await pdfApi.postDocumentPagesRotate(
11+
document, angle, pages, null, tempFolder);
12+
13+
if (response.body.code == 200) {
14+
console.log("rotatePages(): Page successfully rotated.");
15+
await PdfChangeLayoutHelper.downloadResult(document, localFolder, tempFolder, "rotated_output_");
16+
}
17+
else
18+
console.error("rotatePages(): Can't rotate pdf document pages!")
19+
}
20+
21+
},
22+
};

0 commit comments

Comments
 (0)