HOWTO build a ps2dxf-Printer

done by using pstoedit, perl and cups

The main reason to build this environment was the scaling issue after exporting dxf from my drawings. So you will get exactly the same result by using
pstoedit \
-xscale 25.40001209040575503313 \
-yscale 25.40001209040575503313 \
-f dxf_s \
INPUT_POSTSCRIPT_FILE OUTPUT_DXF_FILE

But, while we automate the production, convenient automated code production is only a suggestion, isn't it?

As long as the postscript holds parsable paths we are able from any application to: For better understanding how it works check out use

table of contents

  1. dependencies
  2. cups-config
  3. the printer
  4. run
  5. use
  6. source

1. dependencies

2. cups-config

create your new printer with:
lpadmin -p dxf-file -E -v socket://localhost:12000 -m raw

You should end up with a printer in your /etc/cups/printers.conf similar to this:
<Printer dxf-file>
   Info dxf-file
   DeviceURI socket://localhost:12000
   State Idle
   StateTime 1228779022
   Accepting Yes
   Shared Yes
   JobSheets none none
   QuotaPeriod 0
   PageLimit 0
   KLimit 0
   OpPolicy default
   ErrorPolicy stop-printer
</Printer>

3. the printer

download the perl-code here (you need to rename the file to dxf-printer.pl and make it executable
#!/usr/bin/perl -w -T
use IO::Socket::INET;
use IPC::Open2;
use Gtk2 -init;
use strict;

# copyright Wulf Coulmann <scripts at gpl.coulmann dot de>
# GNU GPL
# http://www.gnu.org/licenses/gpl.html
# Download me here: http://gpl.coulmann.de/cnc/dxf-printer.html



$ENV{'PATH'}= '';

# --- config ------

my $myport = 12000;
my $scale = 25.40009;
my $convert = "/usr/bin/pstoedit -xscale $scale -yscale $scale -f dxf_s ";

chdir '~/';

# --- go for it ---

my $pserve=IO::Socket::INET->new(LocalPort => $myport,Type=>SOCK_STREAM,Reuse=>1,Listen=>1) or die "can't do that $!\n";

while (my $pjob=$pserve->accept()) {

  my $file;
  my $d = Gtk2::FileChooserDialog->new ("file to store dxf", undef, "save",
                                        'gtk-cancel' => 'cancel',
                                        'gtk-save' => 'ok',
                                        );
  $d->set_default_response ('ok');

  if ('ok' eq $d->run ()) {
         $file = $d->get_filename();
  }
  $d->destroy;
  #sleep 2;
  next unless length($file) > 0;


  $file =~ m/^(.*\/).*?$/;
  my $dir = $1;
  print "$dir\n";
  chdir $dir;

  print "now we print \n";
  open FILTER, "| $convert - $file";
  my $input;
  while (<$pjob>) {
    $input .= "$_";
  }
  print FILTER $input;


  close FILTER ;

  close $pjob;
  print "done\n";
}


4. run

Before you can use your new printer you need to start your perl-script. In case of the GTK2 Dialog the script needs a proper environment for both $DISPLAY and $XAUTHORITY. The easiest way to handle this is to start the script as the current logged in X-User with:
./dxf-printer.pl &

There is no harm in keeping the script running in background forever. If you want to start the script at your system startup, make sure you provide the correct display environment.

5. use

For usage you simple go to the print menu in your application (inkscape, xpdf, sodipodi etc.) and select the printer named dxf-file. The GTK file dialog will show up and ask for the directory for the output. As long as you do not restart the perl script the printer will remember the last used directory in which you had placed your file.
Of course, I know that some programs have native dxf-export build in, but most of them will end up scaling differences as I had already mentioned.

5. source

The homepage for this howto is http://gpl.coulmann.de/cnc/dxf-printer.html. Maybe you like to check out http://gpl.coulmann.de