pdf2gerb: Convert PDF to Gerber and NC Drill formats

A few months ago, I had a problem — I wanted to design a printed circuit board in Adobe Illustrator, but also wanted to send it out to BatchPCB for manufacturing. Board houses require that designs be submitted in Gerber format, the creation of which generally requires special CAD software. At first I thought my only recourse would be to etch the board at home, but I did a little research and came to the conclusion that converting PDF files to Gerber format was a viable option.

After a week of Perl hacking, I had a script that would convert each layer of an uncompressed PDF 1.3 file to its corresponding Gerber and NC Drill files, ready for submission to any PCB manufacturer. The result is pdf2gerb, which you’ve found here.

How does it work?

It turns out that the PDF description of vector artwork is surprisingly similar to the Gerber representation, and for good reason. PDF files contain lines of text which move a pen from point to point, drawing lines and changing stroke weights. Similarly, Gerber files contain lines of text which move a photoplotter head around, exposing lines and changing aperture dimensions. Converting from one format is simply a matter of understanding the grammar.

For example, to move to a coordinate (350, 450) in a PDF file, I’d write “350 450 m”. In a Gerber file, I’d write “X350Y450D02″. “.5 w” in a PDF file changes the current stroke weight to .5 points, while “G54D10″ in a Gerber file changes the current aperture to a predefined size specified in the header. By stepping through the PDF file line by line and using regular expressions to match and translate commands between the two grammars, I can build up an equivalent drawing in Gerber format.

What about the drill file?

Simply building up a pattern of copper traces only gets us halfway — we still need to tell the manufacturer’s CAM hardware where to drill holes for our component leads. This is done by specifying an NC Drill file, which (like a Gerber file) lists the drill size and coordinates for each hole in our board.

I’ve chosen to represent drill holes as solid white circles in Illustrator — they look correct when placed atop solid black circles (which represent solder pads and are converted to plotter flash commands). Each white circle in the PDF artwork is converted to a correctly-sized drill command in the NC Drill file.

How do we handle multiple layers?

Gerber and NC Drill files are created for each layer in the PDF file. Because manufacturers expect the bottom layer to line up nicely with the top layer, you can create a proper-looking, top-down board in Illustrator and output it directly to Gerber format.

An example

Here is a board I designed in Illustrator, converted to Gerber using the pdf2gerb script, and sent off for printing to BatchPCB:

To accomplish this, I did the following:

  1. Did a rough layout of the sketches by hand on graph paper
  2. Transferred the sketches into properly-dimensioned layouts in Illustrator
    • Created several layers: Top Copper, Bottom Copper, Top Silkscreen, Soldermask, and Drill.
    • Drew holes in the ‘drill’ layer as a solid black circle (for the pad) with a solid white circle on top (for the drill location and diameter)
    • Used the pen tool on the Top and Bottom layers to connect pads with appropriately-weighted traces
    • Drew solid black circles in the Soldermask layer to cover each pad
    • Drew a rectangle around the board in the Silkscreen layer
  3. Copied the Drill layer into both top and bottom layers (which may not have been necessary)
  4. Saved the document as a copy in PDF format
    • Selected PDF 1.3 format
    • Unchecked “Preserve Illustrator Editing Capabilities”
    • Unchecked “Embed Page Thumbnails”
    • On the Compression page, unchecked “Compress Text and Line Art”
  5. Ran ‘pdf2gerb /path/to/circuit.pdf’
  6. Renamed ‘Top Copper.drd’ to ‘Drill.drd’
  7. Made a .zip file of Top Copper.grb, Bottom Copper.grb, Top Silkscreen.grb, Soldermask.grb, and Drill.drd
  8. Submitted the .zip file to BatchPCB

You can download a .zip containing the files from each step here.

Support, licensing, and caveats

Unfortunately, I cannot provide full-time support for this script… I am employed in an unrelated industry, and I wrote this script for my own use to accomplish a specific purpose. However, I am releasing the source code for this script under the GNU GPL 3.0 — for more details, please read this document. It is my hope that the community will continue to improve this script without my direct involvement.

There are a few things that the script (purposely or otherwise) does not support — text and curved bézier paths, for example. Since this script was developed on Mac OS X, users wishing to run it on Windows will likely need to change the path separator character. I also have not tested its compatibility with PDF output from other applications; Ghostscript may be able to transform other PDF files into a version compatible with this script.

Where do I download pdf2gerb?

You can download the script here. Right-click on the download link and choose “Save Linked File As…”, then save it do the desktop. Next, open a Terminal window, navigate to the desktop folder, and type ‘chmod 755 pdf2gerb.pl’ to mark it as an executable file.

To use pdf2gerb, navigate to the directory that contains it and type ‘pdf2gerb /path/to/circuit.pdf’. Gerber and drill files will be output to the PDF file directory.

Update: pdf2gerb has been updated to version 1.1 and now supports PDF output from Illustrator CS3, which frequently uses the cm command to transform the coordinate plane. It also approximates bezier curves as two straight lines rather than ignoring them.

Update 2: pdf2gerb 1.2 is now available with support for users who, due to application limitations, cannot output filled circles in their PDF. pdf2gerb will now turn stroked circles into a drill hole and copper pad as shown in this diagram. Note that the stacked-filled-circles approach is still preferred, as it gives you the most control over hole and pad size.

35 Comments

  1. step
    Posted September 17, 2007 at 3:42 am | Permalink | Reply

    great!
    i understand why curved beziers are a problem, but aren’t circles from illustrator beziers as well? anyway. i waited for something like this and will soon test it. wonderful it’s pearl, and even better it’s os x by default :-)

    best regards,
    step

  2. Posted September 17, 2007 at 5:32 am | Permalink | Reply

    This is awesome, but you made me really curious — why did you specifically want to design this one in Illustrator and not use a CAD/CAE tool to design it? There are free, there are cheap EE tools like KiCad, FreePCB or Eagle?

  3. Posted September 17, 2007 at 7:04 am | Permalink | Reply

    @step: Yep, circles are bézier paths — but they translate into a single Gerber command that just takes the radius and position. Much easier than stepping through a polynomial function to determine what the path looks like (though it wouldn’t be burdensome to add).

    @svo: The motivation to use Adobe Illustrator is threefold. First, I’ve already spent the time to learn its user interface… some of the CAD apps I looked at were particularly unintuitive. Second, I use Mac OS X, and that significantly limits the number of PCB applications available to me. Finally, the free apps that I found either fell into the unintuitive bucket or had a board size restriction that wouldn’t work for my application.

    As a hobbyist, it was important for me to get from paper to board quickly and cheaply. It only took a couple of hours to write this script (after I’d learned the PDF and Gerber grammars), and I learned much in the process.

  4. Posted September 17, 2007 at 10:48 am | Permalink | Reply

    This is kinda cool, but a program like ‘pcb’ for Linux does this out of the box. Open source, no limitations. But I guess it’s more fun to use a Mac! :-)

  5. Posted September 17, 2007 at 11:21 am | Permalink | Reply

    Russell,

    I have PCB (and the rest of the gEDA suite) on my Mac, but I couldn’t abide its UI. Since producing PCBs isn’t something I’ll do often, I had a hard time convincing myself that overcoming the learning curve was worth the effort.

    The idea here is to free hobbyists and artists from learning a new CAD app and allow them to use a vector art tool of their choice.

  6. Posted September 17, 2007 at 4:12 pm | Permalink | Reply

    Thanks Matt, that sounds reasonable enough to me :) And I agree that gEDA is unbearable and should not exist.
    Great work!

  7. Posted September 17, 2007 at 5:51 pm | Permalink | Reply

    svo: And I agree that gEDA is unbearable and should not exist.

    LOL, though I wouldn’t go quite that far. It’s a strong suite, it just needs some usability enhancements!

  8. step
    Posted September 17, 2007 at 11:00 pm | Permalink | Reply

    @matt: so if beziers other than circles are possible, pleaaaaase go that way or hint me in the right direction. i gotta send you a one of my pcb designs. i didn’t finish them because they were “unproducable” being done in illustrator…. beziers would be the killing feature for pdf2gerb ! and one reason for not using anything else than illustrator to do board design… at least for me

    anyway great great work

    //step

  9. Posted September 17, 2007 at 11:09 pm | Permalink | Reply

    You might be able to work around the lack of bézier curve support by approximating the curve with straight lines… eg instead of a smooth curve, make it using 8-10 straight segments.

    If you were to add bézier curve support to pdf2gerb, the Adobe PDF documentation has the function that you’d use. Plug the numbers from the curve into the formula, then iterate through the function to get (x,y) coordinates and plot them out.

  10. Jose Hidalgo
    Posted December 26, 2007 at 4:51 pm | Permalink | Reply

    Hi Matt. Like other people, I have some trouble running this useful script on OS X (10.3.9). The script seems to work for 1-2 seconds, then nothing happens and there’s no output at all.

    Here’s what my Terminal shows in debug mode (as you will see there are no weird file paths, and something seems to happen on line 82… but it’s not considered as an error ?) :

    Last login: Thu Dec 27 01:44:22 on ttyp1
    Welcome to Darwin!
    [iMac-G4-Jose:~] joe% cd /Users/joe/Desktop
    [iMac-G4-Jose:~/Desktop] joe% perl -d pdf2gerb.pl /Users/joe/Desktop/pdf2gerb/desym.pdf

    Loading DB routines from perl5db.pl version 1.22
    Editor support available.

    Enter h or `h h’ for help, or `man perldebug’ for more help.

    main::(pdf2gerb.pl:24): @apertures = ();
    DB s
    main::(pdf2gerb.pl:27): @drillApertures = ();
    DB
    main::(pdf2gerb.pl:30): @layerTitles = ();
    DB
    main::(pdf2gerb.pl:33): $scaleFactor = 0.0138888889;
    DB
    main::(pdf2gerb.pl:36): $maxBytes = 5 * 1024 * 1024; # 5 MB
    DB
    main::(pdf2gerb.pl:38): if (@ARGV != 1)
    main::(pdf2gerb.pl:39): {
    DB
    main::(pdf2gerb.pl:45): $pdfFilePath = $ARGV[0];
    DB
    main::(pdf2gerb.pl:48): $pdfFilePath =~ /^(.+)\/.+$/;
    DB
    main::(pdf2gerb.pl:51): if ($1 eq “”)
    main::(pdf2gerb.pl:52): {
    DB
    main::(pdf2gerb.pl:57): $outputDir = $1 . “/”;
    DB
    main::(pdf2gerb.pl:61): open pdfFile, “< $pdfFilePath”;
    DB
    main::(pdf2gerb.pl:64): read(pdfFile, $rawPdfContents, $maxBytes);
    DB
    main::(pdf2gerb.pl:67): @rawLines = split /(\r\n|\n\r|\n|\r)/, $rawPdfContents;
    DB
    main::(pdf2gerb.pl:68): chomp(@rawLines);
    DB
    main::(pdf2gerb.pl:69): $pdfContents = join(“\n”, @rawLines);
    DB
    main::(pdf2gerb.pl:70): $pdfContents =~ s/\r//gs;
    DB
    main::(pdf2gerb.pl:71): $pdfContents =~ s/\n\n/\n/gs;
    DB
    main::(pdf2gerb.pl:74): while ($pdfContents =~ /\/Title\((.+?)\)/gs) {
    DB
    main::(pdf2gerb.pl:79): $currentLayer = 0;
    DB
    main::(pdf2gerb.pl:82): while ($pdfContents =~ /BDC(.*?)EMC/gs) {
    DB
    Debugged program terminated. Use q to quit or R to restart,
    use O inhibit_exit to avoid stopping after program termination,
    h q, h R or h O to get additional info.
    DB

    Many thanks in advance for your help. Best regards.

    Jose

  11. Posted December 27, 2007 at 10:14 am | Permalink | Reply

    Jose, I sent you an email — if you feel comfortable sharing the PDF file you’re importing, I can take a look and maybe help debug this. My initial guess is that there are no BDC..EMC blocks in the PDF file… perhaps it was saved in a compressed format?

  12. Andrew Jukes
    Posted February 25, 2008 at 4:07 pm | Permalink | Reply

    Hi Matt

    I am wondering if you were able to sort out Jose’s problem as I think I have hit the same one. Saving my pcb design from Illustrator 12.0.1 and following your step by step instructions doesn’t seem to produce a file with BDC..EMC blocks, so pdf2gerb won’t work. I am running MacOS 10.4.11.

    The script is a great idea. Hope you can help me as at the moment I can’t see any other way forward.

    Regards

    Andrew

  13. Posted February 25, 2008 at 5:19 pm | Permalink | Reply

    Andrew,

    The most common reason for this to happen is forgetting to uncheck “Compress Text and Line Art” in the PDF export dialog… could you double-check that this is off when you’re doing the export?

    I’m also using Illustrator 12.0.1.

    Alternatively, if you’d like to share the .ai file I can give it a go to make sure it exports fine – whatever you’re comfortable with.

  14. Posted February 26, 2008 at 7:32 am | Permalink | Reply

    Andrew,

    I poked around your .ai file a bit — I was able to repro the issue with no BDC/EBC blocks appearing in the PDF output. It turns out that there are several “Live Paint Groups” in the file… when I deleted these, I was able to save a working PDF file. Looks like these groups used transparency (?) and were causing the PDF writer to flatten the entire document into one layer before saving.

    I was able to save the document in PDF 1.5 or 1.6 format and check the “create top-level layers” box to create proper-looking layers without deleting these groups, but I didn’t try converting the resulting document to Gerber. It would probably work fine, but no guarantees…

  15. derick
    Posted May 16, 2008 at 6:12 pm | Permalink | Reply

    I’ve been using Illustrator 10 under Wine on Linux for quite some time. I use it successfully when desingning DXF and eps files for 3d and prototyping. Its a great desing tool!
    I was happy to find something that helps Illustrator work for PCB design as well! However, while trying to run the script on Ubuntu Linux i have not been able to achieve correct results. There are no error messages…The files are created, but gerbviewer shows inaccurate results (I can send files for review)
    Is it possible that I need a newer Illustrator version to work on the designs ? Has anyone been successful with Illustrator 10 for PC?
    Thanks guys!

    Derick

  16. Posted June 4, 2008 at 10:50 pm | Permalink | Reply

    Eagle 5 is native to osx and creates gerbers just fine.

  17. Posted June 5, 2008 at 7:39 am | Permalink | Reply

    Indeed it does, but the UI is atrocious. I tried it and figured there had to be a better way — where “better” is defined as “using vector drawing tools I’m already familiar with”.

  18. Michael Fleming
    Posted June 18, 2008 at 7:55 am | Permalink | Reply

    This is terrific. I use CorelDraw to quickly design RF, analog, and digital boards. Then I send them out to a gerber conversion house as eps. I can quickly do things in CorelDraw that would be very difficult in PCB CAD.

    I will try and use this to eliminate the conversion house — Thank You!

  19. Posted June 18, 2008 at 8:36 am | Permalink | Reply

    You’re welcome and good luck!

  20. safir
    Posted November 7, 2008 at 3:56 am | Permalink | Reply

    I have a pdf with the following format

    Creator: Adobe InDesign CS2 (4.0.5)
    Producer: Adobe PDF Library 7.0
    Security: No
    Format: PDF-1.4
    Optimised: Yes

    and i wish to achieve the same convert it to gerber data files. Can any one please tell me how to go about it.

  21. Posted January 13, 2009 at 4:36 pm | Permalink | Reply

    I am attempting to use this with EasyDraw and TurboCad for Mac OS 10. I think EasyDraw is pretty much a flat-PDF only system when it exports (though it does PDF v. 1.3. TurboCad can export what appears to be a non-flat, non-compressed ASCII PDF file, though in version 1.4. I am working with a very simple drawing to test the concept. May I send you the TurboCAD-generated PDF to see if there is some way I could make this work?

  22. Linus
    Posted January 15, 2009 at 1:08 am | Permalink | Reply

    Could you please teach me how I can run pdf2gerb in Windows XP? It’s reall a good tool. I am just wondering if someone can write a program to convert ai file to gerber file. Then I find this website. But I am using Windows XP and I have a problem to use it. Please help. Thanks a lot.

  23. Posted January 23, 2009 at 8:38 am | Permalink | Reply

    Note: I worked with Richard to get pdf2gerb working with TurboCAD, per his comment above. The result is pdf2gerb 1.2, which supports stroked circles and is compatible with his TurboCAD output.

  24. Urvish
    Posted March 26, 2009 at 11:02 am | Permalink | Reply

    Ok Im new to mac so please bear with me. I dl’ed the file and got the file to mount to an exectutable although i had to leave out the .pl. If i double click on pdf2gerb it opens a terminal window and says:

    Last login: Wed Mar 25 18:46:23 on ttys001
    /Users/urvishkumbhani/Desktop/pdf2gerb ; exit;
    urvish-kumbhanis-macbook-pro:~ urvishkumbhani$ /Users/urvishkumbhani/Desktop/pdf2gerb ; exit;
    usage: pdf2gerb

    logout

    [Process completed]

    The file i want to convert in on my desktop in a folder called attachments. how do i convert it? Typing what you said in the instruction does nothing but give an error saying file does not exists. What do i type, and where, in a new terminal window or in the one that the pdf2gerb opens when i click on it?

  25. Vicky
    Posted March 27, 2009 at 6:56 am | Permalink | Reply

    I would truly like to be able to use this application, but I have tried to use “chmod 755 pdf2gerb.pl” to mark the file as an executable file and it does not work. I add the extension “exe” to the file and type “pdf2gerb/path/to/circuit.pdf” and it seems to be trying to do something, but it works for hours with no resulting gerber files. It does not end and may not be working at all.
    Can you help?

  26. Posted March 27, 2009 at 7:05 am | Permalink | Reply

    @Urvish, Vicky — try this:

    - Download pdf2gerb to your Desktop (don’t rename it)
    - Find the PDF you’d like to convert and copy it to your Desktop
    - Open the Terminal app from Applications -> Utilities
    - Type ‘cd Desktop’ (without the quotes) and hit enter
    - Type ‘pdf2gerb thenameofyourfile.pdf’ and hit enter (note the space between pdf2gerb and the filename)

    Let me know if that works for you. Cheers!

  27. Urvish
    Posted March 30, 2009 at 8:19 am | Permalink | Reply

    Ok tried it again with a fresh dl of pdf2gerb,, idnot rename. This is what i get:

    Last login: Mon Mar 30 09:15:06 on ttys000
    urvish-kumbhanis-macbook-pro:~ urvishkumbhani$ cd desktop
    urvish-kumbhanis-macbook-pro:desktop urvishkumbhani$ pdf2gerb attachments/bar.pdf
    -bash: pdf2gerb: command not found
    urvish-kumbhanis-macbook-pro:desktop urvishkumbhani$

    also tried it with the original renamed program and same problem? Any more suggestions? I have a job that came in so i need to find a way to convert this ASAP. Thanks for the help so far.

  28. Randall
    Posted April 2, 2009 at 9:38 am | Permalink | Reply

    Did anyone ever find out if the pdf2gerb (1.2) script will work and how it would work with Windows XP?? Thanks in advance for any information on this subject.

  29. Linus
    Posted April 2, 2009 at 4:24 pm | Permalink | Reply

    Hello Randall,
    About running pdf2gerb in Windows XP, I have found one way.

    1. Download Activeperl and install it
       http://www.activestate.com/activeperl/

    2.In the Command window (start\run\cmd), run pdf2gerb as:
       C:\Temp\Perl pdf2gerb.pl Circuit.pdf

    3.If you would like to edit the script, there is a convenient tool “Notepad++”
       http://notepad-plus.sourceforge.net/tw/site.htm

  30. Steve Wida
    Posted May 1, 2009 at 8:26 am | Permalink | Reply

    I don’t have Adobe Illustrator, but I do have the ability to pint to a .pdf file from my PCB layout software (Each layer would be in a single .pdf file). Is there a way to convert a single pdf. to a Gerber file using pdf2gerb?

    BTW: I am running this on Windows XP using ActiveState.

    http://www.activestate.com/activeperl

  31. Posted May 1, 2009 at 9:54 pm | Permalink | Reply

    @Steve — many PCB software packages also produce Gerber output. That’s likely to be a better route than pdf2gerb for this case.

    If you still want to give pdf2gerb a try, make sure that the PDF output from your PCB software is in an uncompressed format (looks like lines of text when you open it in Notepad) and that your drill holes are represented as white filled circles atop black filled circles.

  32. Mark Kupferberg
    Posted June 10, 2009 at 1:30 pm | Permalink | Reply

    Can this script be used to convert PDF’s created from scans of mylar PCB fabrication drawings into gerber files? Any other ideas to doing this kind of conversion?

  33. Posted June 11, 2009 at 8:52 pm | Permalink | Reply

    @Mark — probably not, as I’m guessing the PDF output from the scanner is a bitmap image rather than vector line art. Eg if you zoom way in, it looks jaggy rather than smooth… right?

  34. atk
    Posted July 15, 2009 at 2:30 am | Permalink | Reply

    I have a pdf with the following format

    Creator: Adobe inlustrator CS2
    Producer: Adobe PDF Library 7.0
    Security: No
    Format: PDF-1.4
    Optimised: Yes
    Mac osx 10.5
    I do that
    Download pdf2gerb to your Desktop (don’t rename it)
    - Find the PDF you’d like to convert and copy it to your Desktop
    - Open the Terminal app from Applications -> Utilities
    - Type ‘cd Desktop’ (without the quotes) and hit enter
    - Type ‘pdf2gerb thenameofyourfile.pdf’ and hit enter (note the space between pdf2gerb and the filename)
    But it dont found pdf2gerb. you can help me please???. Can any one please tell me how to go about it. I can design on inlustrator.
    Thank you very much!

  35. T. Aldoorn
    Posted October 12, 2009 at 8:26 am | Permalink | Reply

    In the past we used Coreldraw fo design of PCB’s, but firm to make the PCB’s does noe excist aymore.The companies who make PCB’s require Gerber data. Can you make an offer for a program to convert Coreldraw intoe Gerber?

2 Trackbacks

  1. [...] pdf2gerb: Convert PDF to Gerber and NC Drill formats « scattershot genius; – Convert PDFs to Gerber/NC Drill [...]

  2. By Finally! « scattershot genius; on June 5, 2008 at 6:37 pm

    [...] Matt Swann on photography, electronic design, and other miscellany Skip to content AboutProjectspdf2gerb: Convert PDF to Gerber and NC Drill formatstxt2booklet: Generate directories with Perl and XHTMLScriptSaver: [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*