Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(pdf): step form (#15532)

Talal Jaberba6b0288a23f
diff --git a/bun.lock b/bun.lock
index b9b3210018c..77f33076c0e 100644
--- a/bun.lock
+++ b/bun.lock
@@ -10508,7 +10508,7 @@
},
"packages/pieces/core/pdf": {
"name": "@activepieces/piece-pdf",
- "version": "0.5.12",
+ "version": "0.6.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
diff --git a/packages/pieces/core/pdf/package.json b/packages/pieces/core/pdf/package.json
index 05d7e27999b..bb6d2482b3e 100644
--- a/packages/pieces/core/pdf/package.json
+++ b/packages/pieces/core/pdf/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-pdf",
- "version": "0.5.12",
+ "version": "0.6.0",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/core/pdf/src/index.ts b/packages/pieces/core/pdf/src/index.ts
index 08979a2b282..2ee368665da 100644
--- a/packages/pieces/core/pdf/src/index.ts
+++ b/packages/pieces/core/pdf/src/index.ts
@@ -13,7 +13,7 @@ import { addImageToPdf } from './lib/actions/add-image-to-pdf';
export const PDF = createPiece({
displayName: 'PDF',
auth: PieceAuth.None(),
- minimumSupportedRelease: '0.34.2',
+ minimumSupportedRelease: '0.88.2',
logoUrl: 'https://cdn.activepieces.com/pieces/pdf.svg',
authors: [
'nyamkamunhjin',
diff --git a/packages/pieces/core/pdf/src/lib/actions/add-image-to-pdf.ts b/packages/pieces/core/pdf/src/lib/actions/add-image-to-pdf.ts
index 1af08e27425..e2a92de42b9 100644
--- a/packages/pieces/core/pdf/src/lib/actions/add-image-to-pdf.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/add-image-to-pdf.ts
@@ -9,49 +9,50 @@ export const addImageToPdf = createAction({
name: 'addImageToPdf',
classification: 'READ',
displayName: 'Add Image to PDF',
- description: 'Stamps one or more images (PNG or JPG) at exact pixel distances from the top-left corner.',
+ description: 'Stamp one or more images onto pages of an existing PDF.',
aiMetadata: { description: 'Stamps one or more PNG or JPEG images onto an existing PDF at exact point offsets from the top-left corner, each item targeting either a single page or every page. Use it for signatures, logos or watermarks — prefer Add Text to PDF for text, and Image to PDF to turn an image into a standalone document. Each image must have a positive scale and fit entirely within the page bounds, otherwise the call fails; the input file is never modified and repeating the call produces the same stamped content, so idempotent.', idempotent: true },
outputSchema: addImageToPdfActionOutputSchema,
props: {
file: Property.File({
displayName: 'PDF File or URL',
+ placeholder: 'https://example.com/document.pdf',
required: true,
}),
imageItems: Property.Array({
- displayName: 'Image Items to Insert',
- description: 'Add each image you want to stamp.',
+ displayName: 'Images to Stamp',
+ description: 'One item per image. Each can target one page or all pages.',
required: true,
properties: {
imageFile: Property.File({
- displayName: 'Image File (PNG/JPG)',
- description: 'The signature or image file to insert.',
+ displayName: 'Image File or URL',
+ description: 'PNG or JPEG.',
required: true,
}),
applyToAllPages: Property.Checkbox({
- displayName: 'Apply to all pages?',
- description: 'If checked, this image is stamped on every page.',
+ displayName: 'Apply to All Pages',
+ description: 'Stamp this item on every page and ignore Page Number.',
required: false,
defaultValue: false,
}),
pageNumber: Property.Number({
displayName: 'Page Number',
- description: 'Which page to stamp? (Leave blank or ignore if applying to all pages)',
+ description: 'Pages start at 1. Ignored when Apply to All Pages is on.',
required: false,
defaultValue: 1,
}),
distanceFromLeft: Property.Number({
- displayName: 'Distance from Left Edge (in pixels)',
- description: '0 is the far left edge of the page. Standard A4 width is about 595 pts.',
+ displayName: 'Distance from Left',
+ description: 'In points from the left edge. An A4 page is 595 points wide.',
required: true,
}),
distanceFromTop: Property.Number({
- displayName: 'Distance from Top Edge (in pixels)',
- description: '0 is the very top edge of the page. Standard A4 height is about 842 pts.',
+ displayName: 'Distance from Top',
+ description: 'In points from the top edge. An A4 page is 842 points tall.',
required: true,
}),
scale: Property.Number({
- displayName: 'Image Scale',
- description: 'Scale multiplier for the image (e.g., 0.5 for half size, 1.0 for original size).',
+ displayName: 'Scale',
+ description: '1 keeps the original size, 0.5 halves it.',
required: true,
defaultValue: 1.0,
}),
diff --git a/packages/pieces/core/pdf/src/lib/actions/add-text-to-pdf.ts b/packages/pieces/core/pdf/src/lib/actions/add-text-to-pdf.ts
index 16fd8fbc69d..8913eda0b64 100644
--- a/packages/pieces/core/pdf/src/lib/actions/add-text-to-pdf.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/add-text-to-pdf.ts
@@ -13,17 +13,18 @@ export const addTextToPdf = createAction({
name: 'addTextToPdf',
classification: 'READ',
displayName: 'Add Text to PDF',
- description: 'Stamps one or more text strings at exact pixel distances from the top-left corner.',
+ description: 'Stamp one or more text strings onto pages of an existing PDF.',
aiMetadata: { description: 'Stamps one or more text strings onto an existing PDF at exact point offsets from the top-left corner, each item targeting either a single page or every page. Use it for headers, dates or reference numbers — prefer Add Image to PDF for signatures and logos, and Text to PDF to build a document from scratch. Only the 14 standard PDF fonts are available and the call fails if any text would run off a page edge; the input file is never modified and repeating the call produces the same stamped content, so idempotent.', idempotent: true },
outputSchema: addTextToPdfActionOutputSchema,
props: {
file: Property.File({
displayName: 'PDF File or URL',
+ placeholder: 'https://example.com/document.pdf',
required: true,
}),
textItems: Property.Array({
- displayName: 'Text Items to Insert',
- description: 'Add each piece of text you want to stamp.',
+ displayName: 'Text to Stamp',
+ description: 'One item per text block. Each can target one page or all pages.',
required: true,
properties: {
text: Property.LongText({
@@ -31,30 +32,30 @@ export const addTextToPdf = createAction({
required: true,
}),
applyToAllPages: Property.Checkbox({
- displayName: 'Apply to all pages?',
- description: 'If checked, this text is stamped on every page.',
+ displayName: 'Apply to All Pages',
+ description: 'Stamp this item on every page and ignore Page Number.',
required: false,
defaultValue: false,
}),
pageNumber: Property.Number({
displayName: 'Page Number',
- description: 'Which page to stamp? (Leave blank or ignore if applying to all pages)',
+ description: 'Pages start at 1. Ignored when Apply to All Pages is on.',
required: false,
defaultValue: 1,
}),
distanceFromLeft: Property.Number({
- displayName: 'Distance from Left Edge (in pixels)',
- description: '0 is the far left edge of the page. Standard A4 width is about 595 pts.',
+ displayName: 'Distance from Left',
+ description: 'In points from the left edge. An A4 page is 595 points wide.',
required: true,
}),
distanceFromTop: Property.Number({
- displayName: 'Distance from Top Edge (in pixels)',
- description: '0 is the very top edge of the page. Standard A4 height is about 842 pts.',
+ displayName: 'Distance from Top',
+ description: 'In points from the top edge. An A4 page is 842 points tall.',
required: true,
}),
font: Property.StaticDropdown({
displayName: 'Font',
- description: 'Select the exact font variant for this text item.',
+ description: 'One of the 14 standard PDF fonts.',
required: true,
defaultValue: StandardFonts.Helvetica,
options: {
@@ -63,12 +64,13 @@ export const addTextToPdf = createAction({
}),
fontSize: Property.Number({
displayName: 'Font Size',
+ description: 'In points.',
required: true,
defaultValue: 11,
}),
lineSpacing: Property.Number({
displayName: 'Line Spacing',
- description: 'The vertical spacing multiplier between lines. (Examples: 1.0 = Single, 1.15 = Standard, 2.0 = Double)',
+ description: 'Multiplier between lines: 1 single, 1.15 standard, 2 double.',
required: false,
defaultValue: 1.15,
}),
diff --git a/packages/pieces/core/pdf/src/lib/actions/convert-to-image.ts b/packages/pieces/core/pdf/src/lib/actions/convert-to-image.ts
index c0b6ec937a8..016323dcb6e 100644
--- a/packages/pieces/core/pdf/src/lib/actions/convert-to-image.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/convert-to-image.ts
@@ -37,7 +37,7 @@ async function convertPdfToImages(dataBuffer: Buffer): Promise<Buffer[]> {
const files = await fs.readdir(outputDir);
const imageBuffers = [];
- for (const file of files) {
+ for (const file of [...files].sort()) {
const filePath = join(outputDir, file);
const imageBuffer = await fs.readFile(filePath);
await fs.unlink(filePath);
@@ -72,16 +72,18 @@ export const convertToImage = createAction({
name: 'convertToImage',
classification: 'READ',
displayName: 'Convert to Image',
- description: 'Convert a PDF file or URL to an image',
+ description: 'Render a PDF file or URL as PNG images.',
aiMetadata: { description: 'Rasterizes a PDF to PNG images, either as one tall combined image or as a separate image per page (selected via Output Image Type). Pick this when a downstream vision or OCR step needs pixels rather than a text layer; prefer Extract Text for text-based PDFs and Image to PDF for the reverse direction. Requires the pdftoppm binary on the worker and a PDF no larger than 16 MB; the render is deterministic, so idempotent.', idempotent: true },
outputSchema: convertToImageActionOutputSchema,
props: {
file: Property.File({
displayName: 'PDF File or URL',
required: true,
+ placeholder: 'https://example.com/document.pdf',
}),
imageOutputType: Property.StaticDropdown({
displayName: 'Output Image Type',
+ description: 'One tall image of all pages, or one image per page.',
required: true,
options: {
options: [
diff --git a/packages/pieces/core/pdf/src/lib/actions/extract-pdf-pages.ts b/packages/pieces/core/pdf/src/lib/actions/extract-pdf-pages.ts
index c34bf6b3f95..0ad645e7c43 100644
--- a/packages/pieces/core/pdf/src/lib/actions/extract-pdf-pages.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/extract-pdf-pages.ts
@@ -1,6 +1,5 @@
-import { createAction, Property } from '@activepieces/pieces-framework';
+import { createAction, MarkdownVariant, Property } from '@activepieces/pieces-framework';
import { PDFDocument } from 'pdf-lib';
-import { MarkdownVariant } from '@activepieces/pieces-framework';
import { extractPdfPagesActionOutputSchema } from '../output-schemas';
export function pageRangeToIndexes(
@@ -18,7 +17,7 @@ export function pageRangeToIndexes(
throw Error('Range start/end has to be a non-zero number');
}
- if (startPage > totalPages || endPage > totalPages) {
+ if (Math.abs(startPage) > totalPages || Math.abs(endPage) > totalPages) {
throw Error(
'Range start/end has to be less or equal to the total number of pages'
);
@@ -49,15 +48,11 @@ export function pageRangeToIndexes(
}
const markdownValue = `
-This action can extract or rearrange the pages in a PDF.
+Ranges are copied in the order listed, so pages can be reordered.
-- The order of array determines the sequence of pages.
-- Each array element is one inclusive continuous range with a start page and end page.
-- Pages start from 1, and 0 is not valid.
-- Start page has to be less than end page.
-- You can select one page by setting the same start and end page.
-- To select pages from the start, specify negative pages eg. -1 is the last page, -5 is the 5th last page. start: -5, end: -1 are the last 5 pages.
-- Range cannot span across 0 eg. start: -3, end: 5.
+- Pages start at 1. Start and end are inclusive; use the same number for one page.
+- Negative numbers count from the end: -1 is the last page, -5 to -1 the last five.
+- A range cannot cross 0.
`;
export const extractPdfPages = createAction({
@@ -65,7 +60,7 @@ export const extractPdfPages = createAction({
name: 'extractPdfPages',
classification: 'READ',
displayName: 'Extract PDF Pages',
- description: 'Extract or rearrange page(s)from PDF File.',
+ description: 'Copy page ranges from a PDF into a new file, in the order given.',
aiMetadata: { description: 'Builds a new PDF from page ranges taken from a source PDF; because ranges are copied in the order given, it both extracts/trims and reorders pages. Use it to split or resequence one document — use Merge PDFs to join separate files, and PDF Page Count first if bounds are unknown. Ranges are 1-indexed and inclusive, negatives count from the end (start -5, end -1 is the last five pages), and no range may span across 0; the source file is untouched and repeating the call produces the same page content, so idempotent.', idempotent: true },
outputSchema: extractPdfPagesActionOutputSchema,
props: {
@@ -76,16 +71,20 @@ export const extractPdfPages = createAction({
file: Property.File({
displayName: 'PDF File or URL',
required: true,
+ placeholder: 'https://example.com/document.pdf',
}),
pageRanges: Property.Array({
displayName: 'Page Ranges',
+ description: 'One item per range. Order here is the page order in the output.',
properties: {
startPage: Property.Number({
displayName: 'Start Page',
+ description: 'First page of the range. Pages start at 1.',
required: true,
}),
endPage: Property.Number({
displayName: 'End Page',
+ description: 'Last page of the range, inclusive.',
required: true,
}),
},
diff --git a/packages/pieces/core/pdf/src/lib/actions/extract-text.ts b/packages/pieces/core/pdf/src/lib/actions/extract-text.ts
index 0b8f8cfb587..3bb4e8763de 100644
--- a/packages/pieces/core/pdf/src/lib/actions/extract-text.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/extract-text.ts
@@ -7,12 +7,13 @@ export const extractText = createAction({
name: 'extractText',
classification: 'READ',
displayName: 'Extract Text',
- description: 'Extract text from PDF file or url',
+ description: 'Extract the text layer from a PDF file or URL.',
aiMetadata: { description: 'Extracts the text layer of a PDF supplied as an uploaded file or a URL, merging every page into a single string. Use this to read a text-based PDF before parsing, classifying or summarizing it; for a scanned/rasterized PDF use Convert to Image plus an OCR or vision step instead, and use PDF Page Count when only the document length matters. Requires a loadable PDF and returns nothing useful when the pages are just embedded scan images; read-only and idempotent.', idempotent: true },
outputSchema: extractTextActionOutputSchema,
props: {
file: Property.File({
displayName: 'PDF File or URL',
+ placeholder: 'https://example.com/document.pdf',
required: true,
}),
},
diff --git a/packages/pieces/core/pdf/src/lib/actions/image-to-pdf.ts b/packages/pieces/core/pdf/src/lib/actions/image-to-pdf.ts
index 38d8663cfda..470455f7e5f 100644
--- a/packages/pieces/core/pdf/src/lib/actions/image-to-pdf.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/image-to-pdf.ts
@@ -7,14 +7,14 @@ export const imageToPdf = createAction({
name: 'imageToPdf',
classification: 'READ',
displayName: 'Image to PDF',
- description: 'Convert image to PDF',
+ description: 'Wrap a PNG or JPEG image in a one-page A4 PDF.',
aiMetadata: { description: 'Wraps a single PNG or JPEG image into a new one-page A4 PDF, scaling it to fit inside the margins and correcting the EXIF orientation. Use it to make an image attachable or printable as a document; use Add Image to PDF to stamp an image onto an existing PDF, and Merge PDFs to combine several single-image results. Accepts one image per call and only the png/jpg/jpeg extensions; layout and page size are fixed and repeating the call produces the same document content, so idempotent.', idempotent: true },
outputSchema: imageToPdfActionOutputSchema,
props: {
image: Property.File({
- displayName: 'image',
- description:
- 'Image has to be png, jpeg or jpg and it will be scaled down to fit the page when image is larger than an A4 page',
+ displayName: 'Image File or URL',
+ description: 'PNG or JPEG. Large images are scaled down to fit the page.',
+ placeholder: 'https://example.com/photo.png',
required: true,
}),
},
@@ -118,7 +118,7 @@ export const imageToPdf = createAction({
fileName: `${image.filename}.pdf`,
});
} catch (error) {
- throw new Error(`Failed to convert text to PDF: ${(error as Error).message}`);
+ throw new Error(`Failed to convert image to PDF: ${(error as Error).message}`);
}
},
});
diff --git a/packages/pieces/core/pdf/src/lib/actions/merge-pdfs.ts b/packages/pieces/core/pdf/src/lib/actions/merge-pdfs.ts
index 0e9cd2cf958..e56251ac252 100644
--- a/packages/pieces/core/pdf/src/lib/actions/merge-pdfs.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/merge-pdfs.ts
@@ -7,13 +7,13 @@ export const mergePdfs = createAction({
name: 'mergePdfs',
classification: 'READ',
displayName: 'Merge PDFs',
- description: 'Merges multiple PDF files into a single PDF document.',
+ description: 'Merge two or more PDF files into one document.',
aiMetadata: { description: 'Concatenates two or more PDF files into one new PDF, keeping the order in which the files are listed. Use it to join separate documents; use Extract PDF Pages to pull out or resequence pages inside a single document, and Image to PDF first if a source is an image rather than a PDF. Requires at least 2 files and each filename must end in .pdf; the source files are never modified and repeating the call produces the same merged content, so idempotent.', idempotent: true },
outputSchema: mergePdfsActionOutputSchema,
props: {
pdfFiles: Property.Array({
displayName: 'PDF Files',
- description: 'Array of PDF files to merge',
+ description: 'Add the files in the order they should appear.',
required: true,
properties: {
file: Property.File({
@@ -24,9 +24,10 @@ export const mergePdfs = createAction({
}),
outputFileName: Property.ShortText({
displayName: 'Output File Name',
- description: 'Name for the merged PDF file (without extension)',
+ description: 'File name for the merged PDF, without the extension.',
required: false,
defaultValue: 'merged-document',
+ advanced: true,
}),
},
async run(context) {
diff --git a/packages/pieces/core/pdf/src/lib/actions/pdf-page-count.ts b/packages/pieces/core/pdf/src/lib/actions/pdf-page-count.ts
index d2810b0dc83..7df20e00866 100644
--- a/packages/pieces/core/pdf/src/lib/actions/pdf-page-count.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/pdf-page-count.ts
@@ -7,12 +7,13 @@ export const pdfPageCount = createAction({
name: 'pdfPageCount',
classification: 'READ',
displayName: 'PDF Page Count',
- description: 'Get page count of PDF file.',
+ description: 'Count the pages in a PDF file or URL.',
aiMetadata: { description: 'Returns the number of pages in a PDF given as an uploaded file or a URL. Use it as a cheap pre-check before any paging, splitting or looping logic — Extract PDF Pages performs the actual splitting and Extract Text pulls the content. Requires a loadable, non-encrypted PDF; read-only and idempotent.', idempotent: true },
outputSchema: pdfPageCountActionOutputSchema,
props: {
file: Property.File({
displayName: 'PDF File or URL',
+ placeholder: 'https://example.com/document.pdf',
required: true,
}),
},
diff --git a/packages/pieces/core/pdf/src/lib/actions/text-to-pdf.ts b/packages/pieces/core/pdf/src/lib/actions/text-to-pdf.ts
index 4174435be66..492350d373c 100644
--- a/packages/pieces/core/pdf/src/lib/actions/text-to-pdf.ts
+++ b/packages/pieces/core/pdf/src/lib/actions/text-to-pdf.ts
@@ -7,13 +7,13 @@ export const textToPdf = createAction({
name: 'textToPdf',
classification: 'READ',
displayName: 'Text to PDF',
- description: 'Convert text to PDF',
+ description: 'Create a new A4 PDF from plain text.',
aiMetadata: { description: 'Renders a plain-text string into a brand-new A4 PDF, word-wrapping and paginating automatically at a fixed Helvetica 12pt layout. Use it to turn text into an attachable document; use Add Text to PDF to stamp text onto an existing PDF instead, and Image to PDF when the source is an image. Text is the only input and newlines are treated as paragraph breaks; the same text always produces the same document content, so idempotent.', idempotent: true },
outputSchema: textToPdfActionOutputSchema,
props: {
text: Property.LongText({
- displayName: 'text',
- description: 'Enter text to convert',
+ displayName: 'Text',
+ description: 'Line breaks start a new paragraph. Helvetica 12 pt on A4.',
required: true,
}),
},
diff --git a/packages/pieces/core/pdf/src/lib/common.ts b/packages/pieces/core/pdf/src/lib/common.ts
index 16425d7b8a9..d7539780d7a 100644
--- a/packages/pieces/core/pdf/src/lib/common.ts
+++ b/packages/pieces/core/pdf/src/lib/common.ts
@@ -16,7 +16,7 @@ export function getTargetPages(
}
if (pageNumber === undefined) {
- throw new Error(`Page Number is required when "Apply to all pages?" is not checked for ${itemName}.`);
+ throw new Error(`Page Number is required when "Apply to All Pages" is not checked for ${itemName}.`);
}
const pageIndex = Number(pageNumber) - 1;
diff --git a/packages/pieces/core/pdf/test/convert-to-image.test.ts b/packages/pieces/core/pdf/test/convert-to-image.test.ts
new file mode 100644
index 00000000000..9a599cc2d1e
--- /dev/null
+++ b/packages/pieces/core/pdf/test/convert-to-image.test.ts
@@ -0,0 +1,101 @@
+import { basename } from 'path';
+import Jimp from 'jimp';
+
+const renderedFiles = vi.hoisted(() => new Map<string, Buffer>());
+
+vi.mock('fs', async () => {
+ const actual = await vi.importActual<typeof import('fs')>('fs');
+ return {
+ ...actual,
+ promises: {
+ ...actual.promises,
+ mkdir: async () => undefined,
+ writeFile: async () => undefined,
+ readdir: async () => [...renderedFiles.keys()],
+ readFile: async (filePath: string) => renderedFiles.get(basename(filePath)),
+ unlink: async () => undefined,
+ rm: async () => undefined,
+ },
+ };
+});
+
+vi.mock('child_process', () => ({
+ exec: (
+ command: string,
+ callback: (error: Error | null, result: { stdout: string; stderr: string }) => void
+ ) => {
+ const stdout = command.startsWith('command -v') ? '/usr/bin/pdftoppm\n' : '';
+ callback(null, { stdout, stderr: '' });
+ },
+}));
+
+const PAGE_SIZE = 4;
+const PAGE_COLORS = [0xff0000ff, 0x00ff00ff, 0x0000ffff];
+
+async function makePagePng(color: number): Promise<Buffer> {
+ const image = new Jimp(PAGE_SIZE, PAGE_SIZE, color);
+ return image.getBufferAsync(Jimp.MIME_PNG);
+}
+
+async function seedRenderedPagesOutOfOrder(): Promise<Buffer[]> {
+ const pages = await Promise.all(PAGE_COLORS.map((color) => makePagePng(color)));
+ renderedFiles.clear();
+ renderedFiles.set('output-3.png', pages[2]);
+ renderedFiles.set('output-1.png', pages[0]);
+ renderedFiles.set('output-2.png', pages[1]);
+ return pages;
+}
+
+function createMockContext(propsValue: Record<string, unknown>) {
+ const writtenFiles: Array<{ data: Buffer; fileName: string }> = [];
+ return {
+ propsValue,
+ files: {
+ write: async (params: { data: Buffer; fileName: string }) => {
+ writtenFiles.push(params);
+ return `file:///${params.fileName}`;
+ },
+ },
+ writtenFiles,
+ };
+}
+
+describe('convertToImage page order', () => {
+ test('writes one image per page in page order when the directory listing is unsorted', async () => {
+ const { convertToImage } = await import('../src/lib/actions/convert-to-image');
+ const pages = await seedRenderedPagesOutOfOrder();
+ const ctx = createMockContext({
+ file: { filename: 'test.pdf', extension: 'pdf', data: Buffer.from('pdf') },
+ imageOutputType: 'multiple',
+ });
+
+ const result = await convertToImage.run(ctx as any);
+
+ expect(result).toStrictEqual({
+ images: [
+ 'file:///converted_image_page_1.png',
+ 'file:///converted_image_page_2.png',
+ 'file:///converted_image_page_3.png',
+ ],
+ });
+ expect(ctx.writtenFiles.map((file) => file.data)).toStrictEqual(pages);
+ });
+
+ test('stacks pages top to bottom in page order in the combined image', async () => {
+ const { convertToImage } = await import('../src/lib/actions/convert-to-image');
+ await seedRenderedPagesOutOfOrder();
+ const ctx = createMockContext({
+ file: { filename: 'test.pdf', extension: 'pdf', data: Buffer.from('pdf') },
+ imageOutputType: 'single',
+ });
+
+ const result = await convertToImage.run(ctx as any);
+
+ expect(result).toStrictEqual({ image: 'file:///converted_image.png' });
+ const combined = await Jimp.read(ctx.writtenFiles[0].data);
+ expect(combined.getHeight()).toBe(PAGE_SIZE * PAGE_COLORS.length);
+ PAGE_COLORS.forEach((color, pageIndex) => {
+ expect(combined.getPixelColor(0, pageIndex * PAGE_SIZE)).toBe(color);
+ });
+ });
+});
diff --git a/packages/pieces/core/pdf/test/extract-pdf-pages.test.ts b/packages/pieces/core/pdf/test/extract-pdf-pages.test.ts
index b679ce52aab..79c214f8661 100644
--- a/packages/pieces/core/pdf/test/extract-pdf-pages.test.ts
+++ b/packages/pieces/core/pdf/test/extract-pdf-pages.test.ts
@@ -56,4 +56,26 @@ describe('pageRangeToIndexes', () => {
const indexes = pageRangeToIndexes(-5, -5, 10);
expect(indexes).toStrictEqual([5]);
});
+
+ test('should accept negative start equal to the total page count', () => {
+ const indexes = pageRangeToIndexes(-10, -10, 10);
+ expect(indexes).toStrictEqual([0]);
+ });
+
+ test('should map the full negative range to every page', () => {
+ const indexes = pageRangeToIndexes(-3, -1, 3);
+ expect(indexes).toStrictEqual([0, 1, 2]);
+ });
+
+ test('should throw error if negative start is beyond the total pages', () => {
+ expect(() => {
+ pageRangeToIndexes(-15, -1, 10);
+ }).toThrow();
+ });
+
+ test('should throw error if negative end is beyond the total pages', () => {
+ expect(() => {
+ pageRangeToIndexes(-15, -12, 10);
+ }).toThrow();
+ });
});