Jump to content

Wikipedia:Graphics Lab/Resources/PDF conversion to SVG

From Wikipedia, the free encyclopedia

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:

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

[edit]

Convert a single page of a pdf to .svg

[edit]
Conversion with Inkscape
[edit]
  1. Download Inkscape from www.inkscape.org (version 0.46 and above)
  2. Download the PDF you want to convert
  3. Run Inkscape
  4. Open the PDF file you want convert in Inkscape (not Acrobat)
  5. Uncheck Embed images on the box that comes up and click OK
  6. Wait a little while as Inkscape converts it
  7. Click File>Save As..
  8. Under Save as type:, choose "Plain SVG (*.svg)"
  9. Click Save in the bottom right corner
  10. Done! You now have an SVG file with the same name as the PDF, but with the .svg extension
  11. Before uploading you may assure its W3C-validity, with tool SVG-check
  12. For checking that it displays properly, upload it first to Test.svg
  13. 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

[edit]

Large 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

[edit]

Inkscape currently doesn't support extracting multiple pages of a .pdf to svg.

Conversion with pdf2svg
[edit]

pdf2svg 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:

  1. 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.
    1. 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).
    2. Windows: Binaries can be downloaded from here.

To convert a pdf:

  1. 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.
  2. 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

[edit]

Inkscape 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:

  1. Open the PDF in Inkscape as described above
  2. Delete any backgrounds or surrounding text/images near the image you are trying to extract
  3. Select the entire image you are trying to extract by holding the SHIFT key and dragging
  4. If your selection has multiple boxes that appear when selected, right click and choose group
  5. Use CTRL+C or Edit>Copy to copy the image
  6. Use CTRL+N or File>New to create a new document
  7. Use CTRL+V or Edit>Paste to paste your image into the new document
  8. 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"
  9. Press CTRL+SHIFT+D or go to File>Document Properties...'
  10. In the Custom size box, click Resize page to content...
  11. 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
  12. Click Resize page to drawing or selection
  13. 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

[edit]

This 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.

Basic navigation in Inkscape

[edit]

If 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

[edit]

Some 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

[edit]

This 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

[edit]

Inkscape 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?

[edit]

I 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.