Skip to content

Fix PDF export by passing filename without .pdf suffix to nbconvert #16651

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CNDY1390
Copy link

Fixes #16590

This PR resolves a long-standing issue where exporting a Jupyter notebook to PDF via the VS Code Jupyter extension fails, despite the PDF being correctly generated in the temporary directory. The extension was searching for an incorrect filename (either missing or duplicating the .pdf extension), leading to a file-not-found error and a failed export.

Background and Motivation

  • As described in issue #16590, the PDF export functionality in the VS Code extension has been broken for years (at least since 2020).
  • This issue is specific to the VS Code extension and does not occur in the web interface or command-line tools (nbconvert).
  • Many users find Jupyter's export feature convenient, but exporting to PDF from VS Code has been unreliable and frustrating. With this fix, the process will become much smoother, especially for beginners.

Root Cause

  • The issue arises because nbconvert automatically appends a .pdf extension to the output filename when the --output parameter already includes it, resulting in a duplicate extension (e.g., tmp-xxxx.pdf.pdf).
  • Consequently, in the export logic within src/notebooks/export/exportBase.node.ts, the code attempts to access a file with an incorrect name, failing to locate the generated file and incorrectly assuming the export has failed due to a file size of 0:
    if ((await this.fs.stat(Uri.file(tempTarget.filePath))).size > 1) {
        await this.fs.copy(Uri.file(tempTarget.filePath), target);
    }

What has been changed

  • Generating PDF files without duplicate .pdf extensions by preventing nbconvert from appending an extra suffix in the --output parameter.

Additional Context

  • Thanks to the community for detailed analysis and temporary workarounds.
  • Note that this implementation has been primarily tested in a Windows environment and may require further adjustments for non-Windows platforms. Please report any issues encountered. 👍

Closes #16590

@CNDY1390
Copy link
Author

Detailed Version:

Root Cause

When the filename passed to the --output parameter of nbconvert already includes the .pdf extension, the tool appends an additional .pdf, resulting in a filename mismatch. The original code only searches for files with the .pdf extension when looking for the output file, leading to a failure to correctly identify the output file and a misjudgment of failure (file size reported as 0), even though the actual output is successful.

Fix Steps

  1. Define Output Channel: In src\extension.node.ts, define a new output channel for logging and debugging information.
  2. Access and Use Channel: In src\notebooks\export\exportBase.node.ts, access this output channel and use it to output debugging information.
  3. Debug Configuration: Use the provided debug configuration to start debugging and ensure that log information can be captured.
  4. Prepare Test File: Create a .ipynb file for testing the export functionality.
  5. Analyze Logs: Check the nbconvert parameters through the output channel and discover that the filename passed to the --output parameter is automatically appended with a .pdf extension, resulting in a filename like .pdf.pdf.
  6. Modify Code: Change the target filename passed to the --output parameter of nbconvert to a version without the extension, avoiding the duplicate addition of the .pdf extension.

Final Solution

Modify the --output value in the nbconvert parameters to ensure that the filename passed does not include the .pdf extension, providing a thorough and concise solution to the filename mismatch bug.

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)
1 participant