Wikipedia:Graphics Lab/Resources/PDF conversion to SVG
![]() | This help page is a how-to guide. It explains concepts or processes used by the Wikipedia community. It is not one of Wikipedia's policies or guidelines, and may reflect varying levels of consensus. |
Before learning how to convert PDF images to SVG images it may be useful to learn how to extract images from PDF documents and create PNG, GIF, and JPG images. By using Adobe Reader many images in PDF documents can be right-clicked, copied, and then pasted into any image editor. A popular, free image editor good for beginners using Microsoft Windows is IrfanView (if you use GNU/Linux you may have GIMP in your distribution). Launch it and paste the image into it. Then use the image editor to save the image in any format. You can also select almost any area of a PDF document with Adobe Reader's cropping and selection tools, and then copy that selected area by right-clicking it. Then paste it into any image editor for conversion to other image formats. The PrintScreen key on your keyboard is a good last resort, too. Click that key, and then open an image editor and click "paste" (usually in the edit menu). Finally, Wikipedia:Graphic Lab/Image workshop can create, extract, or edit almost any image. Just leave a request for help or advice there.
Now here is some info on how to convert PDF image to SVG images. This page covers conversion using free tools. To convert using Adobe Illustrator go to the following two pages:
- Wikipedia:Graphic Lab/Resources/PDF conversion to SVG/Adobe Illustrator
- Wikipedia:Graphic Lab/Images to improve/Archive/Jul 2008#Image:APISmap1.pdf
It takes only a few seconds to convert a PDF to an SVG, using nothing but free software. Go open source and free software! This tutorial assumes familiarity with installing programs and downloading, saving, opening and uploading files.
This is a tutorial in various formats. Some parts are in question-and-answer format, as in FAQs. Parts of this were copied and adapted from this discussion:
See also commons:Category:PDF maps.
How to convert a PDF to SVG
editConvert a single page of a pdf to .svg
editConversion with Inkscape
edit- Download Inkscape from www.inkscape.org (version 0.46 and above)
- Download the PDF you want to convert
- Run Inkscape
- Open the PDF file you want convert in Inkscape (not Acrobat)
- Uncheck Embed images on the box that comes up and click OK
- Wait a little while as Inkscape converts it
- Click File>Save As..
- Under Save as type:, choose "Plain SVG (*.svg)"
- Click Save in the bottom right corner
- Done! You now have an SVG file with the same name as the PDF, but with the .svg extension
- Before uploading you may assure its W3C-validity, with tool SVG-check
- For checking that it displays properly, upload it first to Test.svg
- Upload the SVG to Wikimedia Commons and tag it with
{{Extracted with Inkscape|v}}
Note: Some versions of Inkscape do not have PDF support compiled in; also, text importing does not always produce satisfactory results in Inkscape.
Conversion with dvisvgm
editLarge and complex PDFs such as some of the maps from the USGS are sometimes difficult for PDF2SVG or Inkscape to convert. For PDFs that are not able to be converted by these tools, dvisvgm seems to do a better job, and even results in smaller SVGs in some cases. dvisvgm is often distributed as part of TexLive, and instructions for obtaining it are available on their website.
To convert a PDF to SVG with dvisvgm, run the following command: dvisvgm --pdf --output=file.svg file.pdf
If you want to make your SVG smaller, you can add --optimize=all to dvisvgm and additionally run the resulting SVG through svgcleaner to further shrink the file without perceptual quality loss. The main drawback is that dvisvgm cannot embed fonts from PDF, it can only convert them to paths.
Convert a multiple page pdf to svg
editInkscape currently doesn't support extracting multiple pages of a .pdf to svg.
Conversion with pdf2svg
editpdf2svg is a command line tool which allows you to extract multiple or all pages of a pdf to a series of .svg files. To install:
- Get pdf2svg from Github here, additional info here. If you are using Windows, Linux or FreeBSD or OSX, pdf2svg is installable via the package installer.
- Ubuntu/Debian/Chromebook with Linux support: Use sudo apt-get update followed by sudo apt-get install pdf2svg in the command line. (Note: you can run a 'persistent' version of Ubuntu on a USB stick without installing it on your hard drive).
- Windows: Binaries can be downloaded from here.
To convert a pdf:
- Convert the first page of a PDF file with pdf2svg file.pdf file.svg. To extract all pages of a multiple-page PDF use pdf2svg file.pdf output-%02d.svg all. This generates output files output-00.svg, output-01.svg, etc. where the pattern "%02d" is replaced by the respective two-digit page numbers.
- If necessary use Inkscape to edit the resulting SVG.
Note: It requires that Poppler, Cairo, and X are installed on your system.
Extracting part of a PDF
editInkscape often produces unreasonably large SVG files from PDFs, especially if you are only trying to use a small part of the PDF (such as a single vector image or logo on a larger page). In order to reduce the size of the resulting image, it is almost always better to copy and paste the portion of interest into a new Inkscape document rather than try to delete the unwanted content and crop the canvas. To do this:
- Open the PDF in Inkscape as described above
- Delete any backgrounds or surrounding text/images near the image you are trying to extract
- Select the entire image you are trying to extract by holding the SHIFT key and dragging
- If your selection has multiple boxes that appear when selected, right click and choose group
- Use CTRL+C or Edit>Copy to copy the image
- Use CTRL+N or File>New to create a new document
- Use CTRL+V or Edit>Paste to paste your image into the new document
- Use the X and Y text boxes in the toolbar to position your image on the page. If your image has a transparent background, it is recommended to leave a 2px-10px margin (for example, you might want to set both values to "2"), otherwise set both to "0"
- Press CTRL+SHIFT+D or go to File>Document Properties...'
- In the Custom size box, click Resize page to content...
- If you are leaving a margin around your image, set the units to "px", and set the four margin boxes to the margin you chose earlier
- Click Resize page to drawing or selection
- Save your file as a "Plain SVG" and test and upload as described above
Copying and pasting into a new document can reduce the file size by a large amount, such as in File:LYNX_transportation_logo.svg, which was reduced 99% from 1.22MB to 5kB.
If you can't see the SVG on Wikipedia/Commons
editThis should work for most PDF to SVG conversions, but if it doesn't there are some things you can do:
- Check that you have the 0.46 or later version of Inkscape. Earlier versions do not have PDF support.
- There may be compatibility problems between the Wikipedia / Media rendering engine and Inkscape:
- Select everything (Ctrl-A) and choose the Path>Object to Path command.
- Select everything (Ctrl-A) and choose the Path>Stroke to Path command.
- In the File>Save As.. dialog look for the drop-down menu just above the Save button and save as a Plain SVG.
- If that doesn't work, call your local friendly WikiGraphist at Wikipedia:Graphic Lab/Images to improve or Wikipedia talk:WikiProject Maps.
Basic navigation in Inkscape
editIf you want to take a look at your SVG in Inkscape hold Ctrl and roll the mouse wheel forward and back to zoom in and out. Use the scroll-bars on the right and bottom to scroll.
Warning
editSome PDFs create very "bad" SVG files when converted automatically. A "bad" SVG can be identified because it will convert slowly (more than 5 seconds), it will be very large for an SVG (over 500kB-1MB) and it will render very slowly on WP - there is a long wait while the text and other images on the page are loaded, but the SVG image does not show up. Bad SVGs put a lot of strain on the Wiki servers. If you suspect you have a "bad" file, go to a Wikigraphist for a manual conversion. These will almost always be much better than the automated versions in terms of being smaller, better drawn and easier to translate because the text is created properly.
Images in PDFs
editThis is excellent if the PDF has vector graphics, but not so useful if it has a photograph or raster graphics inside. Inkscape won't magically convert any raster graphics included in the document into vector graphics.
However, Inkscape can help you to extract the photo.
You can tell that a graphic is a raster image in Inkscape 0.47 if after selecting the graphic and choosing Object > Ungroup, you cannot select individual elements. Also, when you click the graphic Inkscape's status bar will show "Image" and its context menu will have an Image Properties item. If the raster graphic is the only element you want from the PDF, there is no point in saving the file as SVG—it isn't a Scalable Vector Graphic. However, Inkscape can give you an exact lossless copy of the original image from the PDF at its original size in pixels if you don't uncheck Embed images when opening the PDF. One way to do so is to use Extensions > Images > Extract Image... Another is to save as SVG, view the SVG in Firefox, right-click on the image in the SVG, choose View Image from the context menu, then use Firefox's File > Save As... to save the image as a PNG file.
For interest
editInkscape is free and open source software. For additional comments see this discussion:
If you have comments about this tutorial or it doesn't work for you, please comment on the talk page.
Can you clarify what a "PDF map" is, for this purpose?
editI feel ignorant - but a quick wiki-search and Google-search did not enlighten me. I know how to put any graphic file into a pdf - but my strong hunch is that it takes more than this to make it a "pdf map"!
- Good Q. An example of a (vector) PDF map ready for conversion to SVG is this. PDFs can contain practically any type of information, the most common ones being text, raster images (ordinary images like bitmaps) and vector graphics. The vector information has to be coded into the PDF as such. Quite a lot of organizations publish vector information (like maps) this way, so it's handy to be able to convert out of PDF, but putting things into PDFs is pointless for us, it doesn't change the type. If you need a raster image converted, best to speak to a WikiGraphist - MissMJ is the resident pro and very approachable.