Include multiple images at once using rst2pdf - rst2pdf

I'm using rst2pdf to collect several images (named A1.png, A2.png, ... etc) from images folder into one pdf file.
to include one image I write the following in file.txt
.. image:: images/A1.png
then run the following in Linux terminal to convert to pdf
cat file.txt | rst2pdf -o file.pdf
is there a way to include all images at once using the name pattern, something like "images/*.png"?
Thank you

I'm not sure if I quite understand what you are trying to do, do you want to convert your images into PDFs? For that I recommend you could try ImageMagick's convert tool https://imagemagick.org/index.php
If you need to include all images in one PDF, then create an rst file with an image directive for each of the images to include, and rst2pdf will produce a PDF with all the images (or any other restructuredtext content) in it.

Related

Wrong whitebalace/colorgrade for arw files in ImageMagick - updated -

I want to convert SONY raw files (.ARW) to jpg with imagemagick.
But there is a problem with the whitebalance (probably).
When I open the files in ACDSee or XNView, they look like the jpg-version off the camera, but when I open them in imagemagick Display, they are much darker and more reddish.
Obviously there are informations about color in the RAW file, but imagemagick cannot interprete them. Is there any way to extract those informations and apply them separately?
I am in the process of writing a tool to automatically download and publish fotos from the camera, therefore I tried imagemagick.NET (AnyCPU, v11.1) - the conversion program works fine, but the color-problem is the same.
Converted with imagemagick:
Converted with XnView (or any other graphics utility)
For anyone coming across this: according to Fred Weinhaus' comment I added this to my VB
Dim settings As New MagickReadSettings
settings.Format = MagickFormat.Arw
settings.SetDefine(MagickFormat.Arw, "use-camera-wb", "true")
Using Image As New MagickImage(input, settings)

Rotating PDF's less than 90 degrees

I'm working with a bunch of PDF files, some of which have been scanned at a bit of an angle. Adobe Acrobat allows me to rotate PDF files by 90 or 180 degrees. But is there a way to rotate a PDF just a few degrees - just enough to make it straighter?
I could perhaps take a screenshot, open it in Photoshop and rotate it, then somehow convert the Photoshop file to a PDF. However, that seems like a really clumsy process.
PDF supports for complete pages only /Rotate values of 90 degrees, because that is (of course) simple. What you need to do is rotate the contents, not the page. So you need to use something which can remake the PDF file for you.
You could use either Ghostscript or MuPDF to do this. Either will require some coding:
MuPDF will require coding in C,
Ghostscript will require you to do some PostScript programming.
Using Ghostscript you would need to define a BeginPage procedure which rotates the content by a small amount and moves the origin of the content slightly as well (because the rotation rotates around the origin, which is at the bottom left, not the centre).
Here is a short utility script for rotating pages (written in Perl). It converts each page of the input PDF to a PDF XObject Form, rotates the form, then outputs the rotated page.
#! /usr/bin/perl
use warnings; use strict;
use PDF::API2;
use Getopt::Long;
my $degrees = 3;
my $scale = 1.0;
my $x = 0;
my $y = 0;
GetOptions ("rotate=i" => \$degrees, "scale=f" => \$scale, "x=f" => \$x, "y=f" => \$y)
or die "usage: $0 IN_PDF OUT_PDF --rotate=DEG --scale=ALPHA --x=POINTS --y=POINTS";
my $infile = shift (#ARGV);
my $outfile = shift (#ARGV);
my $pdf_in = PDF::API2->open($infile);
my $pdf_out = PDF::API2->new;
foreach my $pagenum (1 .. $pdf_in->pages) {
my $page_in = $pdf_in->openpage($pagenum);
#
# create a new page
#
my $page_out = $pdf_out->page(0);
my #mbox = $page_in->get_mediabox;
$page_out->mediabox(#mbox);
my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum);
#
# lay up the input page in the output page
# note that you can adjust the position and scale, if required
#
my $gfx = $page_out->gfx;
$gfx->rotate($degrees);
$gfx->formimage($xo, $x, $y, $scale);
}
$pdf_out->saveas($outfile);
You'll need to ensure the PDF::API2 and Geopt::Long modules are installed from CPAN.
The script by default rotates 3 degrees anticlockwise, this is configurable vi the --rotate options.
There are also -x, -y and --scale options to allow fine adjustments of the positioning and scale of the output pages.
This question has also been asked on unix.stackexchange.com .
Another option is using LaTeX:
\documentclass{standalone}
\usepackage{graphicx}
\begin{document}
\includegraphics[angle=-1.5]{odd-scan}
\end{document}
In this case, I have the file odd-scan.pdf (a slightly rotated one page scan) in the same folder as the LaTeX file rotated.tex with the content above and then I run pdflatex rotated.tex. The output is a file rotated.pdf with the PDF rotated by 1.5 degrees clockwise.
(I assume a *nix-style environment. On Windows, you can follow these instructions in Cygwin, although I think you might have to build MuPDF from source there as it doesn't appear to be in the Cygwin repos. If you don't want to do that and you're okay with rasterizing the PDF, ImageMagick is in the Cygwin repos and can do the whole job if needed—see below.)
MuPDF's mutool utility can do this. Say you have a PDF file rotate_me.pdf and you want a version of it rotated by 20° clockwise written to a file rotated.pdf:
#!/bin/bash
mutool draw -R 20 -o rotated.pdf rotate_me.pdf
(mutool draw docs)
You can also rasterize the PDF using mutool convert, work with the image files, and then create a new PDF from them (this assumes rotate_me.pdf has between a hundred and a thousand pages—edit the %3d to your liking):
#!/bin/bash
# - for whatever reason convert's `rotate` is counter-clockwise
# - %nd is replaced with the page number
mutool convert -O rotate=-20 -o 'rotated_%3d.png' rotate_me.pdf
(mutool convert docs)
Once you've done whatever else you need to do the image files and you're ready to turn them back into a PDF, you can use ImageMagick:
#!/bin/bash
magick convert $(ls | grep -P 'rotated_[0-9]{3}\.png') rotated_finished.pdf
(If you get an error saying the security policy for PDFs doesn't permit this, you may need to edit /etc/ImageMagick-7/policy.xml and comment out or remove the <policy domain="coder" rights="none" pattern="PDF" /> line. Be aware of this Ghostscript pre-v9.24 vulnerability which that security policy may be intended to mitigate. If you're working with files you made yourself, you should be safe here, but you may want to re-enable this policy afterwards depending on your needs and environment. If you're not working with files you made yourself, especially PDFs, be careful, whether you have a pre-v9.24 Ghostscript installed or not. PDF as a format is very complex and offers many different places to squirrel away maliciousness, and practically speaking you can never be 100% confident that the software you're using to work with it is perfectly hardened.)
ImageMagick can also rasterize PDFs on its own, although it's a bit more complicated. For example:
#!/bin/bash
magick convert -density 150 -rotate 20 rotate_me.pdf rotated.pdf
This might look similar to the mutool draw command, but the difference is that ImageMagick will rasterize the input PDF and then use the resulting images to make the output PDF, so you can use all the regular ImageMagick transformations with this command.
Anyway, -density is for DPI. It will default to 72 DPI if you don't pass that argument, which is likely to not look very good. Also, ImageMagick doesn't seem to be quite as smart as MuPDF about margins and things like that as far as PDFs go, so you may need to do more work with it than this to get reasonable output for your use case. If you do have access to both MuPDF and ImageMagick, I think doing the rasterization with MuPDF and then doing further work on the resulting images with ImageMagick tends to give the nicest results with the least work, but of course that may or may not be practical for you.
(magick convert docs)
Rasterization has obvious disadvantages if your PDF is vector-based—increased file size, fixed resolution, loss of flexibility, etc. Also, even if your PDF is already storing raster graphics, you may lose text data or the like from it in the conversion. If the PDF is really horrible, though, sometimes this is the least painful approach. You can OCR it if needed once you've cleaned it up using Tesseract, often with superior results to whatever may have been done before you arrived.
This can be done with cpdf:
cpdf -rotate-contents 5 in.pdf -o out.pdf
(Rotates around the centre of the page by five degrees)
I had this at one time. I don't know how many pages there are that you have.
What I did is print the pages that wear off use a paper cutter to square them up and rescanned them. Hope this helps.
And yes I've try to find some type of program to fix this and I still have not found one .

Pandoc MD to PDF , full size horizontal rule

I'm using pandoc (on windows with miktex)
with markdown i create a horizontal rule, save that as my source.md
---
(or even an hr tag instead)
but when I convert to PDF the horizontal rule is a tiny little line in the middle of the page, centered.
How do I get it to create one from margin to margin like it does when I convert it to HTML?
Edit:
I tried a CSS styled DIV tag that creates a rule but it is completely ignored during a pdf output.
As pointed above, you can redefine the \rule command in your custom LaTeX template to ignore the first argument:
\let\OldRule\rule
\renewcommand{\rule}[2]{\OldRule{\linewidth}{#2}}
The horizontal rule produces the LaTeX code \begin{center}\rule{3in}{0.4pt}\end{center} when using pandoc -t latex. So you might try to redefine the \rule command in your custom Pandoc template.
Other than that, using ConTeXt instead of LaTeX already results in a full-size horizontal rule.
Try $\hrulefill$. This worked for me.

Converting multiple Markdown files with links to PDF

I've written a load of technical documentation in Markdown. I chose to use this for versioning and so we can view in on GitHub.
We now need to share this with external users (who aren't as comfortable with Markdown), so I would like to convert it to PDF.
Gimli seems to be the best tool I have found for the conversion (it uses the same stylesheet as GitHub so it looks the same), however it doesn't convert the links as well. Is there anything that'll also do this?
I don't know with which type of links you have problems (inline links, reference links, HTML links, image links, automatic links...), but you can try to use Pandoc:
pandoc *.md -o result.pdf
This will convert all files with the *.md file extension to a single pdf.
I use named anchor tags in my markdown document. And I make links to these names. For example,
<a name="#1-overview"></a>
##1. Overview
......
Reference: [1. Overview](#1-overview)
Then I use Typora to open the markdown document and export it to PDF. The resulting PDF preserves these internal links properly.
NO!!! You really don't have to do this!!!
Simply " cat *.md > allpages.md " (you may want to organize the order manually or setup each file in a proper name to let cat work) then open the allpages.md then choose print / export in any markdown editor!!!!
You don't have to install ANYTHING!!!!

Sphinx PDF themes

Does the Sphinx documentation tool offer different PDF themes like it offers different HTML themes?
I Googled the issue but can't find an answer, which leads me to believe the answer is 'no'. Still, i thought i'd ask here.
Thanks.
Firstly, Sphinx doesn't generate PDF output by itself, though there are three general methods to get from Sphinx source files to PDF output:
Use the Latex builder, and then a separate tool like latex2pdf to generate the PDF output
Use the Sphinx plugin from the rst2pdf project
Use the rinoh Sphinx builder provided by rinohtype
That being said there is lots of potential for customizing the styling of your PDF output using either method.
When using the latex->pdf method, you can customize your latex output using a number of options in your sphinx config file. See here. This method is somewhat less convenient than the HTML themes that Sphinx uses for HTML generation, though (IMO).
When using rst2pdf you can define your own stylesheet, which is described in more detail in the manual (look under the "Styles" heading). rst2pdf includes a number of stylesheets, which can be combined for various results. And of course, you can also modify them or create your own (they're just JSON files). These stylesheets also support a kind of inheritance, so act more like the Sphinx HTML themes than the previous method.
rinohtype has extensive provisions for styling a document. See the Basic Document Styling and subsequent sections of the rinohtype manual.
There are no predefined themes for PDF output for Sphinx. But LaTex offers a rich set of options to style the document. My problem was to find the proper way to style the document with sphinx. Here the way, which worked for me:
First take a look into the conf.py. There you'll find an option latex_elements. With this option you can add your own LaTex commands to the output. For example:
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
'papersize': 'a4paper',
# The font size ('10pt', '11pt' or '12pt').
'pointsize': '12pt',
'fontpkg': r"""
\PassOptionsToPackage{bookmarksnumbered}{hyperref}
""",
# Additional stuff for the LaTeX preamble.
'preamble': r"""
\usepackage{setspace}
""",
'footer': r"""
""",
'maketitle': r'''
\pagenumbering{arabic}
''',
}
There are a few points important to know.
Use r""" to avoid conflicts with python
Though preamble would be the right point to add \usepackage you can have conflicts with the Sphinx default settings. Look at fontpkg in the example. It is the first include in the .tex output document. If you have to set options for default packages, do it here.
maketitle let you define your own title page. See some latex documentation. I set \pagenumbering there to have the table of contents with arabic numbers, so the real content begins on page "1".
With the right knowledge of Latex commands you can get good theming with a few commands. A good source to find help is https://tex.stackexchange.com/ where most common problems have a solution. But finding the proper Latex commands is much more difficult than to choose a theme as done for HTML.
It might be helpful to take a look in the Tex-Output under ./_build. There you can see, how the latex_elements-options were included in the document.