Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've read the thread Set Default Page Orientation. It is quite dated (2015) but I have some similar troubles in 2018.
Printing from a browser gives me the most grief the most often. Many web-pages seem to print well as portrait. Some, however, require landscape. Sadly, some web-pages are too wide part way through their content and it does not wrap. The content is then truncated and "lost."
I want all of my print requests, regardless of the source application, to sort out the per-document and per-page orietation -- portrait vs. landscape -- and have the printer do the right things. Okular and Evince seem to behave well for many PDF files. However, apps such as browsers (Firefox, Chromium) and email (Thunderbird) appear to want a single page orientation for every page of every print request.
Do I understand that modern linux workstations make everything into a "PDF data" before asking CUPS to send bytes to a printer? Does the app tag the "PDF data" so CUPS knows where it came from or does it simply grab a file from the spool queue?
Folks, I really, REALLY want to understand this but I'd like a two-phase solution. First, tell me something that works. Second, point me at sources for deeper understanding.
Folks, I really, REALLY want to understand this but I'd like a two-phase solution.
Quote:
Okular and Evince seem to behave well for many PDF files.
You mean to make a pdf from a web page, then print it.
Make the .pdf from web page the way you want it. Make something that allows you to select page size, font size etc.
For example Qt version:
Code:
#! /usr/bin/env python
#Run web2pdf.py then answer input prompts
import sys, signal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl, QSizeF
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtPrintSupport import QPrinter
#Change user agent here
uagent = ('Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0')
class WebToPdf(QWebView):
def __init__(self, url, out_file, font_size, options):
QWebView.__init__(self)
#Set user agent for web page request
def agent(self):
return (uagent)
userAgent = QWebPage(self)
userAgent.userAgentForUrl = agent
web = QWebView()
web.setPage(userAgent)
#Set from options dialog
if 'N' in options:
web.settings().setAttribute(
QWebSettings.JavascriptEnabled, False)
web.settings().setAttribute(
QWebSettings.AutoLoadImages, False)
if 'I' in options:
web.settings().setAttribute(
QWebSettings.JavascriptEnabled, False)
if 'S' in options:
web.settings().setAttribute(
QWebSettings.AutoLoadImages, False)
if 'C' or '' in options:
pass
#Set from font_size input
web.settings().globalSettings().setFontSize(
QWebSettings.MinimumFontSize, int(font_size))
web.load(QUrl(url))
#pdf printer
printer = QPrinter()
#Set pdf page size
#Letter 216 × 279|Legal 216 × 356|Tabloid 279 × 432|Ledger 432 × 279
printer.setPaperSize(QSizeF(216, 279), QPrinter.Millimeter) #Letter
#printer.setPaperSize(QSizeF(216, 356), QPrinter.Millimeter) #Legal
#printer.setPaperSize(QSizeF(279, 432), QPrinter.Millimeter) #Tabloid
#printer.setPaperSize(QSizeF(432, 279), QPrinter.Millimeter) #Ledger
printer.setOutputFormat(QPrinter.PdfFormat)
#Set pdf margins
printer.setPageMargins(10,10,10,10,QPrinter.Millimeter)
printer.setOutputFileName(out_file)
def print_it():
web.print_(printer)
QApplication.exit()
web.loadFinished.connect(print_it)
#Keep a log file
with open('web2pdf_log', 'a') as f:
f.write('\nurl: %s\nFile name: %s\nFont size: %s'
'\nOption used: %s\nUser agent: %s\n'
% (url, out_file, font_size, options, uagent))
def main():
app = QApplication(sys.argv)
#Get inputs
#Get url or quit
url = input('\n' 'Source url can be web page or file://myfile.html'
'\n' 'Enter/Paste source url: ')
if url == '':
print ('Error: Need to enter a url')
sys.exit(0)
#Get out file name or set default name
out_file = input('\n' 'Enter output file name (default: out.pdf): ')
if out_file == '':
out_file = 'out.pdf'
#Get font size or set default size
font_size = input('\n' 'Enter font size for web page (default: 16): ')
if font_size == '':
font_size = '16'
if font_size.isdigit():
pass
else:
print ('Error: Font size has to be an integer')
sys.exit(0)
#Choose options dialog, select type or set default type
options = input('\n''Select pdf type: \n\n'
'No images or scripts N \n'
'Images - No scritps I \n'
'Scripts - No images S \n'
'Complete page C \n'
'default: C \n\n'
'Enter one of N I S C: ')
#Change all chars entered to upper case
options_fix_case = options.upper()
if options_fix_case not in ['N', 'I', 'S', 'C', '']:
print ('Error: Invalid option entered')
sys.exit(0)
print_pdf = WebToPdf(url, out_file, font_size, options_fix_case)
print_pdf
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Or an Example gtk version
Code:
#! /usr/bin/env bash
#webpage to pdf with wkhtmltopdf
clear
#Get inputs
read -p "Enter/Paste page url: " url
read -p "Enter output filename, default (out.pdf): " pdffile
read -p "Enter minimum font size, default (20): " fontsize
#If no font size entered or non integer set default
if ! [ "${fontsize//[0-9]}" ]; then
fontsize=20 #Default font size
fi
#If no out filename entered set default
if [ -z "$pdffile" ]; then
pdffile="out.pdf"
fi
#Select pdf type
read -p "
Lower quality/save space L
No script/no images N
With images/no script I
With scripts/no images S
Full images/scripts/background F
Enter L N I S F : " lnif
#Keep a log file
echo "
"$(date)"
URL: "$url"
Outfile name: "$pdffile"
Fontsize: "$fontsize"
Quality: "$lnif"
" 2>&1 | tee -a webtopdf_log
#Get it
case $lnif in
[Ll]* ) wkhtmltopdf -l -T 6 -B 6 -R 3 -L 3 --no-background \
--no-images --disable-javascript --disable-plugins \
--minimum-font-size "$fontsize" "$url" "$pdffile"
;;
[Nn]* ) wkhtmltopdf -T 6 -B 6 -R 3 -L 3 --no-background \
--no-images --disable-javascript --disable-plugins \
--minimum-font-size "$fontsize" "$url" "$pdffile"
;;
[Ii]* ) wkhtmltopdf -T 6 -B 6 -R 3 -L 3 --no-background \
--disable-javascript --disable-plugins \
--minimum-font-size "$fontsize" "$url" "$pdffile"
;;
[Ss]* ) wkhtmltopdf -T 6 -B 6 -R 3 -L 3 --no-background \
--no-images --disable-plugins --minimum-font-size "$fontsize" \
"$url" "$pdffile"
;;
[Ff]* ) wkhtmltopdf -T 6 -B 6 -R 3 -L 3 --javascript-delay 10000 \
--no-stop-slow-scripts --minimum-font-size "$fontsize" \
"$url" "$pdffile"
;;
*) echo "Invalid option"
;;
esac
Then print the .pdf
Edit: I'm sorry wkhtmltopdf is qt5 now also.
Code:
pacman -Si wkhtmltopdf
Repository : community
Name : wkhtmltopdf
Version : 0.12.4-1
Description : Command line tools to render HTML into PDF and various image
formats
Architecture : x86_64
URL : https://wkhtmltopdf.org/
Licenses : LGPL3
Groups : None
Provides : None
Depends On : qt5-webkit qt5-svg
Optional Deps : xorg-server: wkhtmltopdf needs X or Xvfb to operate
Conflicts With : None
Replaces : None
Download Size : 162.42 KiB
Installed Size : 685.00 KiB
You mean to make a pdf from a web page, then print it.
WOW! Thanks for all of that.
I have and will learn a lot from your code on several fronts.
I'm not sure that it "solves" my original problem. Given that I have a browser open to some page that I want to print, the browser has some menu item or keystroke (eg, CTRL-P) that enables PRINT_THIS_PAGE. It is at this point that I'm seeking to intervene and assert control over what gets printed.
I know that I can select PRINT_TO_FILE and make a PDF document from the web page. I can then use your scripts and others to tinker both the output and the printer settings that get used. It sounds like I want my own variant, PRINT_TO_FILE_MyWAY. Alternately, I need to automatically notice that I've created a new PDF document in some folder and launch a chain of scripts to tinker then print that file.
I have the same problem that some websites automatically print as landscape which are really portrait and some print as portrait but then some content is truncated.
Your correct that the default format was changed from Postscript to PDF. You can create your own cups filters to automatically check the PDF but if the content is already truncated before cups and then just printing as landscape will not help much.
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
I have my browser set to 'preview' before it prints. From there I can check the format and switch between orientations. I also usually don't want every individual printed page of a web page, so I specify only the pages I want printed.
I know that I can select PRINT_TO_FILE and make a PDF document from the web page. I can then use your scripts and others to tinker both the output and the printer settings that get used.
No,
Copy the url from the browser and paste it into your pdf making script. The above scripts make a pdf from a url, no cups, no BS. Then after you have a .pdf made from the page, do what you want with it, print it, keep it, delete it...etc.
Unremark the paper size that you want. Or have the script ask you for it.
-B, --margin-bottom <unitreal>
Set the page bottom margin
-L, --margin-left <unitreal>
Set the page left margin
-R, --margin-right <unitreal>
Set the page right margin
-T, --margin-top <unitreal>
Set the page top margin
-O, --orientation <orientation>
Set orientation to Landscape or Portrait
--page-height <unitreal>
Page height
-s, --page-size <Size>
Set paper size to: A4, Letter, etc.
--page-width <unitreal>
It gives you the ability to inspect the pdf before you print it. Or make the pdf 3 or 4 times until you get it right. You can also dload the page to file once, then use the script or wkhtmltopdf on the file. You have to download it anyway to make a pdf from it, regardless how you get it. Make a script like above and you can get a page real quick with/without images, with/without scripts. My 2 cents.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.