Page 1 of 1

How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 8:02 am
by MochiMoppel

My tax office requires all attachments to be in PDF format. I now have various image files in PNG format which I want to convert.
The challenge: The resulting PDFs should have exactly the same dimensions as the source files. Normally PDF printing/converting offers to select a page size, e.g. A4 or letter, and an option to resize the image to the page size. I want the opposite, I want the page size to resize to the image size. A 600x400px image should result in a 600x400px PDF when displayed at 100% in a PDF viewer.

More challenges: Using only tools that are already installed. I'm using BW64 10.0.6 , so often recommended convert or img2pdf are out of the race. Command line tools, if possible.

It's more difficult than I expected.
So far I had success with Abiword and CUPS printing, but the process is tedious and involves defining a page layout or a custom size exactly as large as the image.

Something that works great is inkscape. Not installed but I have an AppImage saved outside of the main sfs and a symlink to this AppImage in my PATH. The command inkscape --export-type="pdf" my_file.png will create a file my_file.png.pdf , with exactly the same dimension. It's also the only tool I know that would convert SVG to PDF.

Finally I tried PeasyPDF, which has a long history in Puppy but hasn't been maintained since its last version 4.2.(here the download page)
@radky has made some changes to the code so that it runs in BW64. For my purposes the "Convert" tool looks promising, however the code contains a syntax error and happily produces an empty PDF file when choosing a "Custom" page size. Did this ever work?

Let's fix it:
Find the lines

Code: Select all

	INWIDTH=$(dc $PIXWIDTH $CUSTOMDPI / p)	#inches
	INHEIGHT=$(dc $PIXHEIGHT $CUSTOMDPI / p)

and change to

Code: Select all

	INWIDTH=$(dc -e"2k $PIXWIDTH $CUSTOMDPI / p")
	INHEIGHT=$(dc -e"2k $PIXHEIGHT $CUSTOMDPI / p")

In order to create a PDF with same size on screen as the image the dpi value for "Custom" needs to be changed.
Let's set "Custom" to 96dpi, the de-facto standard in all Puppies
and "Custom+" to 48dpi, increasing size to 200% of original
and "Custom-" to 192dpi, decreasing size to 50% of original:

Code: Select all

if [ "$PAGESIZE" = "$(gettext 'Custom')" ]; then
	CUSTOMDPI=96;
elif [ "$PAGESIZE" = "$(gettext 'Custom+')" ]; then
	CUSTOMDPI=48;
elif [ "$PAGESIZE" = "$(gettext 'Custom-')" ]; then
	CUSTOMDPI=192;
else
	CUSTOMMODE="no"
fi

BW64 defaults to gtkdialog compiled forGTK3. If the drop down list doesn't work, the original gtkdialog3 in the last line needs to be changed to gtk2dialog.

That's all to achieve my goal. Making PeasyPDF more flexible and user friendly is another goal, but I leave this for a rainy day.


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 1:01 pm
by rcrsn51
MochiMoppel wrote: Mon Apr 29, 2024 8:02 am

Did this ever work?

Yes. But years ago, busybox dc changed its syntax and PeasyPDF was updated accordingly. Puppy continues to use an obsolete version.


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 2:38 pm
by rockedge

Puppy continues to use an obsolete version.

Where is the not obsolete version? If it is to be an included application in future Puppy Linux variants it needs to be accessible for a woof-CE build run from a reachable repository.

I'm not hunting it down.


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 2:39 pm
by Jasper

@MochiMoppel

I thought I had compiled the application "Poppler" which has the ability to convert graphic files to pdf.

https://poppler.freedesktop.org/


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 3:04 pm
by bigphil

In Fossapup their is a utility called PuppyPDF - Menu/document.
It can convert .png files to PDF.


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 3:55 pm
by rcrsn51
rockedge wrote: Mon Apr 29, 2024 2:38 pm

Where is the not obsolete version? If it is to be an included application in future Puppy Linux variants it needs to be accessible for a woof-CE build run from a reachable repository.

It's in the Starter Kit project, available through Fred's repo. But that version is not Puppy-compatible.


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 4:01 pm
by williwaw
rockedge wrote: Mon Apr 29, 2024 2:38 pm

Where is the not obsolete version?

advanced search (gear icon)
peasypdf
rcrsn51


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 4:35 pm
by rockedge

@rcrsn51 Excellent! Thank you.


Re: How to convert PNG image to PDF?

Posted: Mon Apr 29, 2024 4:40 pm
by fredx181
rcrsn51 wrote: Mon Apr 29, 2024 3:55 pm
rockedge wrote: Mon Apr 29, 2024 2:38 pm

Where is the not obsolete version? If it is to be an included application in future Puppy Linux variants it needs to be accessible for a woof-CE build run from a reachable repository.

It's in the Starter Kit project, available through Fred's repo. But that version is not Puppy-compatible.

This one from my repo probably, (gtk3 supported version .deb package from rcrsn51), perhaps someone can make it "Puppy-compatible" :?: https://github.com/doglinux/book-worm/r ... .0_all.deb
(earlier version 4.6 from 2022 by rcrsn51: (for with gtk2); https://github.com/doglinux/book-worm/r ... .6_all.deb)


Re: How to convert PNG image to PDF?

Posted: Tue Apr 30, 2024 12:17 am
by MochiMoppel
fredx181 wrote: Mon Apr 29, 2024 4:40 pm

This one from my repo probably, (gtk3 supported version .deb package from rcrsn51), perhaps someone can make it "Puppy-compatible" :?: https://github.com/doglinux/book-worm/r ... .0_all.deb

Maybe the same person who made it Puppy-incompatible ;) ?
Xdialog3 .. no idea what this is.
I'm surprised how many versions of PeasyPDF are floating around, all with "# rcrsn51 2011-08-09
# version 2.3 2012-11-15" in the header. Makes it hard to keep track.


Re: How to convert PNG image to PDF?

Posted: Tue Apr 30, 2024 12:47 am
by wizard

@rockedge

Here's a peasypdf I hacked to work with BW64:

viewtopic.php?p=109621#p109621

Thanks
wizard


Re: How to convert PNG image to PDF?

Posted: Tue Apr 30, 2024 1:11 am
by MochiMoppel

As for the changes in dc:
I suggest to replace this tool with something more "stable", to make the code backward compatible, e.g. with bc (provided the syntax hasn't changed as well).
Even better - and faster - would be to use POSIX compatible shell arithmetic and substitution.


Re: How to convert PNG image to PDF?

Posted: Sat Jan 11, 2025 7:45 am
by greengeek
MochiMoppel wrote: Mon Apr 29, 2024 8:02 am

Finally I tried PeasyPDF, which has a long history in Puppy but hasn't been maintained since its last version 4.2.(here the download page)
@radky has made some changes to the code so that it runs in BW64. For my purposes the "Convert" tool looks promising, however the code contains a syntax error and happily produces an empty PDF file when choosing a "Custom" page size. Did this ever work?

Let's fix it:
Find the lines

Code: Select all

	INWIDTH=$(dc $PIXWIDTH $CUSTOMDPI / p)	#inches
	INHEIGHT=$(dc $PIXHEIGHT $CUSTOMDPI / p)

and change to

Code: Select all

	INWIDTH=$(dc -e"2k $PIXWIDTH $CUSTOMDPI / p")
	INHEIGHT=$(dc -e"2k $PIXHEIGHT $CUSTOMDPI / p")

In order to create a PDF with same size on screen as the image the dpi value for "Custom" needs to be changed.
Let's set "Custom" to 96dpi, the de-facto standard in all Puppies
and "Custom+" to 48dpi, increasing size to 200% of original
and "Custom-" to 192dpi, decreasing size to 50% of original:

Code: Select all

if [ "$PAGESIZE" = "$(gettext 'Custom')" ]; then
	CUSTOMDPI=96;
elif [ "$PAGESIZE" = "$(gettext 'Custom+')" ]; then
	CUSTOMDPI=48;
elif [ "$PAGESIZE" = "$(gettext 'Custom-')" ]; then
	CUSTOMDPI=192;
else
	CUSTOMMODE="no"
fi

BW64 defaults to gtkdialog compiled forGTK3. If the drop down list doesn't work, the original gtkdialog3 in the last line needs to be changed to gtk2dialog.

That's all to achieve my goal. Making PeasyPDF more flexible and user friendly is another goal, but I leave this for a rainy day.

Thanks for the code changes. I tried to replace the affected code - but still produced an empty image when custom was selected. (Lots of messages in terminal saying "dc" did not recognise 300, or 1024 etc)
Maybe I incorrectly inserted the customdpi code.
Anyway - I was able to get a useable output - but I needed to select A4 and "resize graphic to fit page".

But I wondered if you had a sample copy of the /usr/sbin/peasypdf utility that you had modified per your comments above - that you might make available? Would be nice to test it knowing that the code inserts were correctly placed (as i don't feel 100% that I understood correctly where/how to replace the code)


Re: How to convert PNG image to PDF?

Posted: Sat Jan 11, 2025 8:15 am
by MochiMoppel
greengeek wrote: Sat Jan 11, 2025 7:45 am

But I wondered if you had a sample copy of the /usr/sbin/peasypdf utility that you had modified per your comments above - that you might make available?

Has already been made available.
BW64 10.0.9 ships with an updated version of PeasyPDF that has the changes implemented.

Edit: No, I see that it doesn't ship with the code you quoted. It's even newer and eliminated the need for dc as suggested in my previous post. You don't need to worry about dc errors anymore ;)


Re: How to convert PNG image to PDF?

Posted: Sun Jan 12, 2025 5:24 am
by Geek3579

Some suggestions might be found here: https://stackoverflow.com/questions/232 ... it-to-page

I use Imagemagick all the time on Bookwormpup64-10.3. But one needs to run the following to install it: apt install graphicsmagick-imagemagick-compat
From the link above maybe try: convert -density 80 -page a4 input.png out.pdf


Re: How to convert PNG image to PDF?

Posted: Sun Jan 12, 2025 6:00 am
by MochiMoppel
Geek3579 wrote: Sun Jan 12, 2025 5:24 am

From the link above maybe try: convert -density 80 -page a4 input.png out.pdf

Thanks, I know that convert can do a lot of wonderful things, but as I wrote in my initial post:"often recommended convert or img2pdf are out of the race". Let's keep it simple and use already installed tools if they can do the job ;)