Skip to content

Instantly share code, notes, and snippets.

@skpassegna
Created February 24, 2024 05:27
Show Gist options
  • Save skpassegna/9a72151eaf6c670d3739f36fd6388929 to your computer and use it in GitHub Desktop.
Save skpassegna/9a72151eaf6c670d3739f36fd6388929 to your computer and use it in GitHub Desktop.
This script will convert all SVG files in the current directory to PDF files using Inkscape. (Windows)
@echo off
REM Check if Inkscape is installed
if exist "%ProgramFiles%\Inkscape\bin\inkscape.exe" (
set inkscape_path=%ProgramFiles%\Inkscape\bin\inkscape.exe
) else (
echo Error: Inkscape not found. Please install it from https://inkscape.org/
exit /b 1
)
REM Set output folder (optional)
set output_folder=
REM Loop through all SVG files in the current directory
for %%a in (*.svg) do (
REM Get the filename without extension
set filename=%%~na
REM Build the output filename (use output folder if specified)
if not "%output_folder%"=="" (
set output_file=%output_folder%\%%filename.pdf
) else (
set output_file=%%filename.pdf
)
REM Convert the SVG file to PDF using Inkscape
echo Converting "%%a" to "%output_file%"...
"%inkscape_path%" --export-type="pdf" --export-dpi=300 "%%a" -o "%output_file%"
REM Check for errors (optional)
if errorlevel 1 (
echo Error: Failed to convert "%%a".
)
)
echo Done!
pause
@skpassegna
Copy link
Author

Batch script for converting SVG files to PDF using Inkscape

This script will convert all SVG files in the current directory to PDF files using Inkscape. It includes detailed comments to explain each step.

Requirements:

Instructions:

  1. Save the following code as a .bat file (e.g., convert_svg_to_pdf.bat) in the directory containing your SVG files.
  2. Open a command prompt in the same directory.
  3. Run the batch file by typing its name and pressing Enter (e.g., convert_svg_to_pdf.bat).

Explanation:

  • The script first checks if Inkscape is installed and retrieves its path.
  • You can optionally set an output_folder variable to specify a different destination for the PDF files.
  • The for loop iterates through all SVG files in the current directory.
  • For each file, the script generates the output filename and calls Inkscape with the appropriate commands:
    • --export-type="pdf" specifies the output format.
    • --export-dpi=300 sets the output resolution to 300 DPI (you can adjust this).
    • "%%a" is the input SVG file.
    • -o "%output_file%" specifies the output PDF file.
  • An optional error check is included to identify any conversion failures.
  • The script prints a completion message and pauses the window.

Alternative tools:

If you cannot use Inkscape or batch scripting, consider these alternative online tools.

  • Dedicated software: Adobe Illustrator, CorelDRAW, and other graphics software can also export SVG files to PDF.

I hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment