-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: pdf export #8564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Palanikannan1437
wants to merge
3
commits into
preview
Choose a base branch
from
feat/pdf-export
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,300
−80
Open
fix: pdf export #8564
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { CollaborationController } from "./collaboration.controller"; | ||
| import { DocumentController } from "./document.controller"; | ||
| import { HealthController } from "./health.controller"; | ||
| import { PdfExportController } from "./pdf-export.controller"; | ||
|
|
||
| export const CONTROLLERS = [CollaborationController, DocumentController, HealthController]; | ||
| export const CONTROLLERS = [CollaborationController, DocumentController, HealthController, PdfExportController]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import type { Request, Response } from "express"; | ||
| import { Effect, Schema, Cause } from "effect"; | ||
| import { Controller, Post } from "@plane/decorators"; | ||
| import { logger } from "@plane/logger"; | ||
| import { AppError } from "@/lib/errors"; | ||
| import { PdfExportRequestBody, PdfValidationError, PdfAuthenticationError } from "@/schema/pdf-export"; | ||
| import { PdfExportService, exportToPdf } from "@/services/pdf-export"; | ||
| import type { PdfExportInput } from "@/services/pdf-export"; | ||
|
|
||
| @Controller("/pdf-export") | ||
| export class PdfExportController { | ||
| /** | ||
| * Parses and validates the request, returning a typed input object | ||
| */ | ||
| private parseRequest( | ||
| req: Request, | ||
| requestId: string | ||
| ): Effect.Effect<PdfExportInput, PdfValidationError | PdfAuthenticationError> { | ||
| return Effect.gen(function* () { | ||
| const cookie = req.headers.cookie || ""; | ||
| if (!cookie) { | ||
| return yield* Effect.fail( | ||
| new PdfAuthenticationError({ | ||
| message: "Authentication required", | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| const body = yield* Schema.decodeUnknown(PdfExportRequestBody)(req.body).pipe( | ||
| Effect.mapError( | ||
| (cause) => | ||
| new PdfValidationError({ | ||
| message: "Invalid request body", | ||
| cause, | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| return { | ||
| pageId: body.pageId, | ||
| workspaceSlug: body.workspaceSlug, | ||
| projectId: body.projectId, | ||
| title: body.title, | ||
| author: body.author, | ||
| subject: body.subject, | ||
| pageSize: body.pageSize, | ||
| pageOrientation: body.pageOrientation, | ||
| fileName: body.fileName, | ||
| noAssets: body.noAssets, | ||
| cookie, | ||
| requestId, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Maps domain errors to HTTP responses | ||
| */ | ||
| private mapErrorToHttpResponse(error: unknown): { status: number; error: string } { | ||
| if (error && typeof error === "object" && "_tag" in error) { | ||
| const tag = (error as { _tag: string })._tag; | ||
| const message = (error as { message?: string }).message || "Unknown error"; | ||
|
|
||
| switch (tag) { | ||
| case "PdfValidationError": | ||
| return { status: 400, error: message }; | ||
| case "PdfAuthenticationError": | ||
| return { status: 401, error: message }; | ||
| case "PdfContentFetchError": | ||
| return { | ||
| status: message.includes("not found") ? 404 : 502, | ||
| error: message, | ||
| }; | ||
| case "PdfTimeoutError": | ||
| return { status: 504, error: message }; | ||
| case "PdfGenerationError": | ||
| return { status: 500, error: message }; | ||
| case "PdfMetadataFetchError": | ||
| case "PdfImageProcessingError": | ||
| return { status: 502, error: message }; | ||
| default: | ||
| return { status: 500, error: message }; | ||
| } | ||
| } | ||
| return { status: 500, error: "Failed to generate PDF" }; | ||
| } | ||
|
|
||
| @Post("/") | ||
| async exportToPdf(req: Request, res: Response) { | ||
| const requestId = crypto.randomUUID(); | ||
|
|
||
| const effect = Effect.gen(this, function* () { | ||
| // Parse request | ||
| const input = yield* this.parseRequest(req, requestId); | ||
|
|
||
| // Delegate to service | ||
| return yield* exportToPdf(input); | ||
| }).pipe( | ||
| // Log errors before catching them | ||
| Effect.tapError((error) => Effect.logError("PDF_EXPORT: Export failed", { requestId, error })), | ||
| // Map all tagged errors to HTTP responses | ||
| Effect.catchAll((error) => Effect.succeed(this.mapErrorToHttpResponse(error))), | ||
| // Handle unexpected defects | ||
| Effect.catchAllDefect((defect) => { | ||
| const appError = new AppError(Cause.pretty(Cause.die(defect)), { | ||
| context: { requestId, operation: "exportToPdf" }, | ||
| }); | ||
| logger.error("PDF_EXPORT: Unexpected failure", appError); | ||
| return Effect.succeed({ status: 500, error: "Failed to generate PDF" }); | ||
| }) | ||
| ); | ||
|
|
||
| const result = await Effect.runPromise(Effect.provide(effect, PdfExportService.Default)); | ||
|
|
||
| // Check if result is an error response | ||
| if ("error" in result && "status" in result) { | ||
| return res.status(result.status).json({ message: result.error }); | ||
| } | ||
|
|
||
| // Success - send PDF | ||
| const { pdfBuffer, outputFileName } = result; | ||
|
|
||
| // Sanitize filename for Content-Disposition header to prevent header injection | ||
| const sanitizedFileName = outputFileName | ||
| .replace(/["\\\r\n]/g, "") // Remove quotes, backslashes, and CRLF | ||
| .replace(/[^\x20-\x7E]/g, "_"); // Replace non-ASCII with underscore | ||
|
|
||
| res.setHeader("Content-Type", "application/pdf"); | ||
| res.setHeader( | ||
| "Content-Disposition", | ||
| `attachment; filename="${sanitizedFileName}"; filename*=UTF-8''${encodeURIComponent(outputFileName)}` | ||
| ); | ||
| res.setHeader("Content-Length", pdfBuffer.length); | ||
| return res.send(pdfBuffer); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: makeplane/plane
Length of output: 101
🏁 Script executed:
# Read the apps/live/package.json file to see the actual dependencies at the mentioned lines cat -n apps/live/package.jsonRepository: makeplane/plane
Length of output: 3319
🏁 Script executed:
# Also check the pnpm-workspace.yaml to see if catalog is defined cat pnpm-workspace.yamlRepository: makeplane/plane
Length of output: 994
Add packages to
catalog:in both files.Lines 31–33, 43–45, 52, 58, and 73–80 introduce external packages with version ranges. Per repo policy, external dependencies must use
catalog:with versions declared inpnpm-workspace.yaml. These packages are missing from the catalog and need to be added there before updating package.json references.Update
pnpm-workspace.yamlto include:Then update
apps/live/package.jsonto usecatalog:for these entries, as shown in the original diff.🤖 Prompt for AI Agents