Statically compile pdftk for Heroku. Need to split PDF into single page files
Unfortunately Heroku keeps stripping out magic to add flexibility. As a result it feels more and more like the days when I used to manage and maintain my own servers. There is no easy solution. My "monkey patch" is to send the file to a server that I can install PDFTK, process the file, and send it back. Not great, but it works. Having to deal with this defeats the purpose of using heroku.
Ruby - using pdftk to split a multi page pdf into many single page pdf's?
You can try it using the combine_pdf
gem:
require 'combine_pdf'
pages = CombinePDF.load("my_pdf.pdf").pages;
i = 0
pages.each do |page|
pdf = CombinePDF.new
pdf << page
pdf.save("#{i}.pdf")
i+=1
end
The combine_pdf
gem (I'm the author) is a native Ruby solution, so you don't have to worry about pdftk
's installation and requirements. PDFTK Script to split all files within a folder - noob
Looking at this again, I can't see any reason why you can't do this with simple shell programming. Something like:
for %s in (c:\test*.pdf) do pdftk %s burst output c:\pdftest\%~ns_%02d.pdf
ought to work.
How to set up pdftk or iText to work with Rails 3 on Heroku?
You can use pdftk
on Heroku – it's pre-installed. You use it by shelling out to it as a command-line program from within your Ruby app.
For most pdftk
commands, you will use Tempfile
s. Interpolate the Tempfile#path
s in the arguments to pdftk
.
`pdftk #{subcommand}`
raise Exception unless $?.success?
In some cases, you will be able to pipe data in and out using stdin
and stdout
rather than Tempfile
s.input = ...
output = IO.popen "pdftk #{subcommand}", 'w+b' do |io|
io.write input
io.flush
io.close_write
io.read
end
How to add pdftk to Heroku Cedar app?
I've got this working now, and a publicly available version of a custom Heroku Ruby buildpack with pdftk is here:
https://github.com/millie/heroku-buildpack-ruby-pdftk
Be sure to see the instructions in the README.
Precompiled version of pdftk for Heroku's Cedar stack is available here:
https://github.com/millie/pdftk-source
Hope this saves someone else some trouble!
Insert a blank page between each existing page in a PDF document
Okay I did it myself using PHP and FPDI/FPDF:
<?php
error_reporting(E_ALL);
require_once('fpdi/fpdf.php');
require_once('fpdi/fpdi.php');
// Format für die einzelnen Folien:
$format = 'L'; // Entweder '' (horizontal) oder 'L' (Landscape!)
// Verzeichnis lesen
foreach(glob('infiles/*.pdf') as $file)
{
$filename = basename($file);
$fileout = 'outfiles/' . $filename;
// Ausgabe-PDF
$out = new FPDI();
// Vorhandenes PDF einlesen
$pagecount = $out->setSourceFile($file);
// Alle Seiten nacheinander importieren
for($i = 1; $i <= $pagecount; $i++)
{
// Importiere Seite
$tpl = $out->importPage($i); // , '/MediaBox'
// Vorhandene Seite
$out->addPage($format);
$out->useTemplate($tpl);
if($i < $pagecount)
{
// Leere Seite anfügen (nur nicht am Ende)
$out->addPage($format);
}
}
$out->Output($fileout);
}
all files in the subdirectory 'infiles' will get blank Pages inserted and saved to 'outfiles' with the same filename!
Related Topics
How to Convert JSON to Xml in Ruby
Adding Two Activerecord::Relation Objects
What Is The Purpose of Setting Ruby Block Local Variables When Blocks Have Their Own Scope Already
No Such File to Load - Mechanize
What Hash Function Does Ruby Use
Multistep Form with Activeadmin
How to Remove a Url's Trailing Slash in a Rails App? (In a Seo View)
Sorting Hash of Hashes by Value (And Return The Hash, Not an Array)
Question on Ruby Collect Method
Ruby Require 'File' Doesn't Work But Require './File' Does. Why
In Ruby, Is Truthiness Idiomatic for a Method Name Ending with a Question Mark
Timeout When Installing Ruby Gems
Combining Ruby on Rails and Backbone
Git: Forcing Tests Before Pushing to Local or Remote Master