GM-Script-Writer-62850 / PHP-Scanner-Server

Allows you to use your Linux install as a web based scanner server thus allowing you to scan with any web enabled device; Now supports server side printing! (As of 1.4.11)
59 stars 17 forks source link

Printing to Server CUPS #46

Closed kneiser closed 4 years ago

kneiser commented 4 years ago

I recently built up a new 64-bit Debian test server to replace an aging 32-bit machine. I've been using the PHP Scanner Server software for sometime now. While building the new machine I decided to install CUPS on the to print to 3 printers; Laser (network), Inkjet (network), and CUPS-PDF driver. I can print a test page with no problems from CUPS (v2.2.10) to the printers. However when I scan an image then print it to one of the printers only a "top-left" section of the page prints. If I print to CUPS-PDF again the same resulting output. If I go to the command-line and issue an "lp" command to print the image to one of the printers it prints great! So this seems to me that the scanning software needs to include a "fit-to-page" option issued to CUPS? I have attached a copy of the image with a red box to show the page section that prints. CUPS does not seem to have a default "fit-to-page setting" I have this same problem on my production server.

Scanned Image I have attached a screenshot of the printer setting used: ScannerSetting300dpi I tried different page sizes and get the same output result. Is there a place in the software to force "fit-to-page" or do you have another suggestion what it could be?

GM-Script-Writer-62850 commented 4 years ago

I have 3 different print methods see line 12 of config.ini Which method are you using? Screenshot_2020-06-09_22-29-47

GM-Script-Writer-62850 commented 4 years ago

I tested each method using the cups-pdf printer and was unable to find an issue printing a scanned file only issue i had was when choosing 'i don't care' i got a error related to this https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion and after editing like 94 of /etc/ImageMagick-6/policy.xml it works as expected (using xubuntu 20.04)

BTW your "printer options" you posted that is your scanner setting not your print settings, perhaps you are miss interrupting the UI?

kneiser commented 4 years ago
  1. Printing Method: 1
  2. "printing options", I wanted to show what scanning and output options being used. Oh and btw the default scanning of 50dpi prints on the same page.
  3. Stackoverflow article, nothing changed. I even tried other combinations according to the help comments. Would an ImageMagick policy be restricting the conversion/printing or image/pdf files with larger dpi?
GM-Script-Writer-62850 commented 4 years ago

3 if you look at the debug console and you see a convert command run that involves a pdf file you would get a error, you would not get error message instead of a file, in the case clicking the i do not care button you get a Error.txt file instead of a pdf

I tried again to reproduce it without success using a PNG file with a res of 5098x6598 pixels

** Suddenly tried one last thing and was able to reproduce it When you hit a print button you get a option to use the integrated print your your systems printer, when hitting cancel you get the integrated printer, this works 100% every time, however when you hit ok it loads the image on a page and open's your browsers print dialog, this is where i am able to get this problem, there is NO PDF conversion in this process, it just gives the browser a image on a page and tell the browser you want to print it

I have found 2 issues

  1. Firefox does not scale images to fit a page when printing
  2. the page instantly closes when you try this, the print dialing does not pause the javascirpt and the page closes with user just seeing a tab appear and diapers as if it was a blocked ad

A user with a server set with Printer set to 0 in config.ini will b confused as the only way they have to get a good print is to download a PDF and print said PDF

GM-Script-Writer-62850 commented 4 years ago

further testing with chome shows it will expand the image to a second page vertically, all horizontal overflow is lost tried some CSS to fix it and cups-pdf gave me a corrupt file, save to pdf in chrome works giving me a 5 page file

GM-Script-Writer-62850 commented 4 years ago

I have a few ideas on how to solve this

  1. Print button only supports integrated printer

    • For config.ini Printer = 0 the button will be removed or print button tells you to use the pdf button and click print there
    • For config.ini Printer > 0 the button will open the integrated printer options
  2. Print button opens a PDF file and we trust the user to hit the print button in the pdf

  3. Print button opens a PDF in a iframe and we print the iframe (NOT sure if this is possible)

kneiser commented 4 years ago

You reference the use of a PDF. Most times I will be using the CUPS connected printers (Cancel button) on the server. I am not always planning to print an image to PDF but just to one of the other printers. Unless you avocate that a converting image(s) to PDF is best before printing directly to a CUP connected printer on the server? On the odd occasion I would use my Linux client machine to download selected images scanned using the scanner server PDF button. Please advise.

GM-Script-Writer-62850 commented 4 years ago

When i refer to PDF i am not referring to the PDF made using cups-pdf, i am treating the PDF made using the cups-pdf printer as actual paper, i am referring to the backend of the scanner server UI, when you use the integrated printer the image is converted to a PDF file (in the selected format) and that pdf file is sent to the printer, be it cups-pdf, brother, hp, or xerox printer

There are 3 ways the server will make a PDF to send to the printer

The clarify my testing showed the integrated printer (hitting cancel) produces a good output every time when using the cups-pdf printer (great printer for testing as it does not waste trees and ink/toner) however when using the browser to print (hitting OK) images get cut off and do not span over to the next page as expected (firefox)

kneiser commented 4 years ago

The clarify my testing showed the integrated printer (hitting cancel) produces a good output every time when using the cups-pdf printer (great printer for testing as it does not waste trees and ink/toner) however when using the browser to print (hitting OK) images get cut off and do not span over to the next page as expected (firefox) Sorry if I am causing confusion. I don't get the same results. Additionally I've not made any tweaks to any server configs. Here is the use case I used to print/test a scanned image:

  1. I click on the printer icon in the PHP Scanner toolbar.
  2. When the Cancel/OK dialogue displays I click on the Cancel button.
  3. On the printer selection dialogue I select one of the network printers or cups-pdf.
  4. Results are: a) Network printer prints an upper-left section of the image being printed. b) CUPS-PDF generates a blank page.
  5. Further to the testing. I open a terminal window and go to the directory where the scanned image is stored. I issue an "lp" command to one of the CUPS printers. Image prints perfectly.
GM-Script-Writer-62850 commented 4 years ago

Can you attach a sample scan that this happens with? Does the PDF download work?

kneiser commented 4 years ago

Ok here are two zip files:

The server is running Debian 10 (Buster) current version. testscans.zip policy.xml.zip

GM-Script-Writer-62850 commented 4 years ago

i somehow broke my pdf printer, also noticed my /var partition is full probably related ii am blaming snap packages on this, bit i get to move a few hundred gigs around to make the partition larger... i'll test more after i fix that tomorrow

GM-Script-Writer-62850 commented 4 years ago

Xubuntu 20.04 unable to reproduce, you said this only happens with large files right? cat /etc/php/7.3/apache2/php.ini | grep memory_limit since php spawns the lp command it may be subject to the memory limit of php, mine is set to 128M *Buster uses php 7.3, focal uses php 7.4; i never had issues with php 7.2 here are some other settings that may be different

~$ cat /etc/php/7.3/apache2/php.ini | egrep "^(memory_limit|max_execution_time|post_max_size|upload_max_filesize|html_errors|display_errors)"
max_execution_time = 30
memory_limit = 128M
display_errors = On
html_errors = On
post_max_size = 8M
upload_max_filesize = 25M

I have PrivateTmp disabled, maybe that matters? https://muras.eu/2017/12/06/apache-ubuntu-systemd-privatetmp/

kneiser commented 4 years ago

I use php 7.4.

  1. I set my php memory limit to 128M no change.
  2. I tried disabling PrivateTemp, no change.
  3. You said that php spawns lp? I printed the image from the command line using lp and the output on all printers was perfect. I confirmed again from command line and the scanned image prints perfectly.
GM-Script-Writer-62850 commented 4 years ago

When php runs the lp command it does so from php as www-data, so i would image it has the same permission and memory restrictions you can try running it like this sudo -u www-data lp...

If you insert this above like 33 of printer.php it will give you a script and a pdf to try to reproduce the issue, both files will be in /tmp (requires PrivateTmp to be disabled, if you want to be able to find it)

    @copy($file,"/tmp/WhyDoesItNotWork.pdf");
    $S_file="/tmp/WhyDoesItNotWork.sh";
    $S_file=@fopen($S_file,'w+');
    @fwrite($S_file,'#!/bin/sh'."\n".'sudo -u www-data lp -d '.escapeshellarg($_GET['printer'])." -n $q -o $o /tmp/WhyDoesItNotWork.pdf");
    @fclose($S_file);
kneiser commented 4 years ago

I have PrivateTmp disabled. Put in the code as provided. Same results. printercodeshot I even went to the command line first and issuded the sudo -u... you provided and the image printed perfectly in that case.

GM-Script-Writer-62850 commented 4 years ago

That code will give you file for further debugging, just to be sure nothing fishy is going on, it drops a shell script and a pdf into your server's /tmp folder (if you do not have the files PrivateTmp is enabled)

Here are the files i get tmp.zip

The PDF is used by the script it is NOT the output of cups-pdf

kneiser commented 4 years ago
  1. Just to confirm my PrivateTmp setting: cat /etc/systemd/system/multi-user.target.wants/apache2.service [Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=https://httpd.apache.org/docs/2.4/

[Service] Type=forking Environment=APACHE_STARTED_BY_SYSTEMD=true ExecStart=/usr/sbin/apachectl start ExecStop=/usr/sbin/apachectl stop ExecReload=/usr/sbin/apachectl graceful PrivateTmp=false Restart=on-abort

[Install] WantedBy=multi-user.target

  1. I've attached the 2 files from the server tmp directory. WhyDoesItNotWorkFiles.zip
  2. Use case used. Selected printer item in PHP Scanner toolbar, selected the network Laser printer connected to CUPS server. Same problem result.
GM-Script-Writer-62850 commented 4 years ago

I was not saying you had it on or off, i have had that feature annoy me more than once it really got under my skin the 1st time does it fail when you run the sh script?

kneiser commented 4 years ago

I was just verifying the pretest conditions. No the script did not fail just same output issue.

GM-Script-Writer-62850 commented 4 years ago

if that same the same useless print but it works without the sudo -u www-data I'd say it is a permissions issue is www-data in the lp group? groups www-data if so if you put www-data in your user group does it work

kneiser@busterServer:~$ groups www-data
www-data : www-data lp kneiser

currently installing buster in a VM

kneiser commented 4 years ago
  1. www-data is a member of the lp group.
  2. added to my group. same issue.
GM-Script-Writer-62850 commented 4 years ago

Hopefully the cups and cups-pdf log files will tell me something

GM-Script-Writer-62850 commented 4 years ago

So how did you get it to work for your default user account I get the same issue with www-data, root, and the user account lp -d PDF -n 1 /tmp/WhyDoesItNotWork.pdf

GM-Script-Writer-62850 commented 4 years ago

I found a way to fix it, but i want to know how to detect that this is problem or how to make it not a problem, i do not like dirty patches like this but give this a try 1.6-18a.zip [newer version in next post] there is a option in config.ini, you will know it when you see it for what ever reason if i run the pdf through convert and do nothing to it in theory it makes the file size explode and it works then...

GM-Script-Writer-62850 commented 4 years ago

Think i found a better way than using convert instead of printing a PDF we print a ps file, this takes the conversion time down from 1.248s to 0.351 seconds i think i also found out why it took forever to install debian, virtualbox was set to 1 core Take a look at this: patchResultsComparison.zip

1.6-18a1.zip

I have changed the BusterPrintBug option from a boolean to a integer where 0 = original, 1 = pdf2ps, and 2 = convert

kneiser commented 4 years ago

I reviewed the config.ini. I will try this later today.

kneiser commented 4 years ago

Here are my results of printing a scanned image to either a Server network laser printer or CUPS-PDF. Did I test as expected? config.ini Settings

Scenario 1

Printer = 1 ReplacePrinter = false BusterPrintBug = 1

Same as original issue.

Scenario 2

Printer = 1 ReplacePrinter = false BusterPrintBug = 2

Same as original issue.

Scenario 3

Printer = 1 ReplacePrinter = false BusterPrintBug = 0

Same as original issue.

GM-Script-Writer-62850 commented 4 years ago

Here is me trying to reproduce your issue: Screencast 2020-06-12 16:34:13.mp4.zip Scenarios 1 and 2 should work while 3 produces a trash bin print i used the sample image you provided for this test on a clean install of debian buster

kneiser commented 4 years ago

config.ini settings using cups-pdf for output testing

Scenario 1

Printer = 1 ReplacePrinter = false BusterPrintBug = 1

Produced a PDF file. Looks good.

Scenario 2

Printer = 1 ReplacePrinter = false BusterPrintBug = 2

Produced a PDF file. However, as you predicted the output quality is not good.

Did not bother with Scenario 3 as we agree on the result, trash bin

So using the postscript file output provides results using cups-pdf. So what about usecases where a user wants to print a scanned image(s) directly to a CUPS network connected printer? Would you create a postscript file first then send that to the network connected printer?

GM-Script-Writer-62850 commented 4 years ago

BusterPrintBug = 1 converts the internal PDF to a new PDF and prints said PDF file using the lp command BusterPrintBug = 2 converts the internal PDF to a internal PS (post script) file then prints the PS file using the lp command

So what about usecases where a user wants to print a scanned image(s) directly to a CUPS network connected printer?

You mean just send the image to the printer, the reason i use pdf field in the back end is PDF files print like you would expect, in my exp (until you found this quirk) they just work, now there is the 'i do not care just print it' button which converts the image file to a pdf in full auto mode no rules, no paper sizes, are you saying i should do this? lp -d PDF ../www-data/php-scanner/scans/file/Scan_0_Jun_10_2020~19-35-11.jpg

since you were able to confirm the printed file works using cups-pdf i assume it also works on a real printer?

GM-Script-Writer-62850 commented 4 years ago

I am assuming you are no longer able to get garbage prints with BusterPrintBug > 0 just committed quite a large update with many bug fixes, see the change log for details

kneiser commented 4 years ago

Ok I just installed the new version and did some testing:

  1. One page scanned image outputs correctly now when printing directly to a CUPS network connected printer on the server. PDF generates fine too.
  2. Bulk printing of multiple scanned images to CUPS connected network printer shows the original problem. However, printing multiple images to the CUPS connected cups-pdf outputs correctly. So is there a separate section in the scanner code for multiple printing that needs to be updated? I downloaded the generated PDF and had to scaled the PDF to to scale output on to one page, but I think best to leave this alone.

Would be nice to be able to print mulitple scanned images to a network printer from the server.

GM-Script-Writer-62850 commented 4 years ago

So bulk printing has a issue but bulk pdf is fine?

GM-Script-Writer-62850 commented 4 years ago

If that is what you are saying i can't seem to get it to happen

i did however notice something strange, if i download a pdf and use the print in firefox it works fine but if i open it in evince and print it the print is garbage, this is with BusterPrintBug = 0

also noticed that BusterPrintBug = 1 is MUCH better quality while BusterPrintBug = 2 is horrible, still better than BusterPrintBug = 0 since there is actually content on the print, my original testing showed BusterPrintBug = 1 being worse quality than BusterPrintBug = 2 the only difference in the code that runs when you use the cups-pdf driver is the name of the printer and options the printer has, this stuff is shown in the bottom of this image

testPrints.zip

The server has treats a real printer and the virtual cups-pdf exactly the same, they runs the exact same lines of code, unless you are using the pdf download there is not a single line that is different, even then both are highly integrated, though BusterPrintBug does not apply to PDF downloads

I do consider this a bug in debian buster, i'm just adding a work around for said bug, i did check to see if this was a issue on ubuntu Groovy and was not able to get this problem

kneiser commented 4 years ago

Well for bulk printing I will avoid printing multiple scanned images directly to a server network connected printer. For bulk printing I would mainly use cups-pdf and the output for muliple images is not blank like the original issue.

GM-Script-Writer-62850 commented 4 years ago

Just pushed a update in that will fix the quality using BusterPrintBug 2 BusterPrintBug theoretically supports values of about 9E18 but if you do something silly like that prepare to run out of system resources or hit some other software limit

GM-Script-Writer-62850 commented 4 years ago

I just pushed a update that may fix a secondary issue you were having basically all the printer options I was sending to lp were formatted wrong so they did nothing