# Obtaining Cropped Line Drawings From Simulink

## Motivation

For my third year project I needed a way to put my annoyingly large Simulink system into a document. Screenshots won’t do because the quality is far too low, so I generally avoid putting raster images in reports. There clearly had to be a better way, which I hope show in this short but useful post. These methods will work with MacOS and *nix systems.

At the end of the post is a tool which will give you the ability to split and crop a multi page pdf all in one go, with some basic usage directions.

## Get the PDF

This part is simple enough, press ‘print’ and a dialog box will pop up. Select ‘print to file’ and ‘all systems’, as highlighted in the image below:

This should print a PDF file to some location specified on your system. This is fantastic! We have a line drawing representation of our diagram, albeit in a rather unusable form, we clearly need to split this file up and deal with it more from there.

## Split the PDF

Fortunately we do not have to get too involved with PDF reading, as a tool exists to help us with PDF mainpulation!

We can use pdftk to split our multi-page PDF into single page PDFs by executing the following command:

pdftk multipage.pdf burst

You may have to install pdftk, as it is not guaranteed as standard on *nix.

I really like pdftk, it gives you more tools for PDF manipulation than you would ever need, and it’s free software!

Here is a little aside where I (actually pulled from help files) list some of the answers to the question: What can you do with pdftk?

1. Merge PDF Documents
• Split PDF Pages into a New Document
• Rotate PDF Pages or Documents
• Decrypt Input as Necessary (Password Required)
• Encrypt Output as Desired
• Fill PDF Forms with FDF Data or XFDF Data and/or Flatten Forms
• Apply a Background Watermark or a Foreground Stamp
• Report on PDF Metrics such as Metadata, Bookmarks, and Page Labels
• Attach Files to PDF Pages or the PDF Document
• Unpack PDF Attachments
• Burst a PDF Document into Single Pages
• Uncompress and Re-Compress Page Streams
• Repair Corrupted PDF (Where Possible)

## Crop the PDFs

Again the great UNIX gods pity us, another tool exists which allows us to crop PDF documents!

The tool in question (a perl script!) is the appropriately-named pdfcrop, which comes pre-installed on MacOS and *nix systems.

To use pdfcrop, perform the following command:

pdfcrop singlepage.pdf

pdfcrop will then output a file called singlepage-crop.pdf in the same directory.

Which when put together should give you a nicely cropped simulink line drawing!

## Automating ourselves out of the job

Splitting and cropping that PDF was honestly a very involved process. pdfcrop has to be called for each page, and the number of files involved becomes a small nightmare, especially if you make changes to your plant and need to update your diagrams.

Fortunately I have taken the liberty in writing a small bash script, which can be used as following (after a chmod +x splitcrop.sh).

./splitcrop.sh multipage.pdf

For my Third Year project I ended up using this script, but with some commands to automatically rename the files taked onto the end, here’s one for example:

mv ./split_files/pg_0001-crop.pdf ../report/3yp_general/images/plant/Global.pdf

This may be useful if working with a group project and making lots of small changes, otherwise omit this.

## Splitcrop.sh

#!bin/env/bash

echo "Splitting PDF $1" # split the pdf and move all pages to a split files' folder pdftk$1 burst
mkdir split_files
mv pg_0* ./split_files/

filetype=".pdf"
files=find ./split_files/

# crop each pdf
for file in $files do if [[$file = *${filetype} ]]; then echo "Splitting$file"
pdfcrop \$file
fi
done

# remove non-cropped files
mv ./split_files/*-crop.pdf ./
rm ./split_files/*
mv *-crop.pdf ./split_files/`