Skip to content

Fix PDF export file path handling #16594

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

Closed
wants to merge 2 commits into from
Closed

Conversation

CNDY1390
Copy link

Fixes #16590

Fixes the long-standing bug where exporting a Jupyter notebook to PDF via the VS Code Jupyter extension fails, even though the PDF is correctly generated in the temp directory. The extension was looking for the wrong filename (missing or duplicating the .pdf extension), resulting in a file-not-found error and failed export.

Background and Motivation

  • As detailed in issue #16590, exporting to PDF from the VS Code extension has been broken for years (at least since 2020).
  • The bug is not present in the web interface or command-line tools (nbconvert), only in the VS Code extension.
  • Many users believe Jupyter's export is convenient, but in practice, exporting to PDF from VS Code is unreliable and cumbersome. But if this commit is merged, exporting to PDF from VS Code would be much easier, especially for begginners.

Root Cause

  • The extension generates a PDF file in the temp directory (e.g., tmp-xxxx.pdf), but then tries to access or copy a file with an incorrect name (e.g., tmp-xxxx.pdf.pdf or missing .pdf).
  • This is due to incorrect filename/path handling in the export logic, specifically in src/notebooks/export/exportBase.node.ts:
    if ((await this.fs.stat(Uri.file(tempTarget.filePath))).size > 1) {
        await this.fs.copy(Uri.file(tempTarget.filePath), target);
    }

What has been changed

  • The filename and path handling logic is corrected to ensure the extension always looks for the actual generated PDF file.

Additional context

  • Thanks to the community for detailed analysis and temporary workarounds.
  • My implementation might not be robust for non-Windows environment. Please follow the post if there is any problem. 👍

Closes #16590

@DonJayamanne DonJayamanne requested a review from amunger April 29, 2025 20:48
if ((await this.fs.stat(Uri.file(tempTarget.filePath))).size > 1) {
await this.fs.copy(Uri.file(tempTarget.filePath), target);
let pdfFilePath = tempTarget.filePath;
if (!pdfFilePath.endsWith('.pdf') && (await this.fs.exists(Uri.file(pdfFilePath + '.pdf')))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you check why it creates a file that ends in .pdf.pdf?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I’ve dug deeper into the issue and found that nbconvert automatically appends a .pdf suffix to the --output parameter, causing the filename error. Instead of modifying the judgment function, a better approach is to pass a filename without the suffix. I’ll close this PR and submit a new one with the improved fix.

@CNDY1390 CNDY1390 closed this May 17, 2025
@CNDY1390
Copy link
Author

Hi there, I've opened a new PR with an improved fix for this issue. Please take a look at the updated solution here: #16651. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot export PDF just with vscode extension (other methods work)
2 participants