JavaFx, printing PDF (From PDFBOx) - pdf

I have a JavaFX application that generates a PDF with Apache PDFBox. And I would like to print that PDF.
However, I was not able to find a good solution.
I have tried with the java.awt.print.PrinterJob but it hangs when calling the PrinterJob#printDialog(..) method. The mouse cursor becomes "wait" and I cannot use the application anymore.
I have tried with javafx.print.PrinterJob. It can show the print dialog but I do not find a way to print something else than a node.
At this time, I print with the java.awt.Desktop API. It print my PDF but skip the print dialog.
So, my question is: How can I display the print dialog and print a PDF (ideally an org.apache.pdfbox.pdmodel.PDDocument) from JavaFX?
My project uses Java 17 (Open Jdk) and JavaFX 16. I have the issue on MacOS and never tried on other OS.
Instead of printing the PDDocument I can also write it to he disk and print the File or convert it to any kind of stream.
Thanks a lot

The issue, as enlighted by #Saw, was because I was not trying to open the AWT print dialog from the EDT.
The solution is to wrap that code into a Runnable submitted to SwingUtilities.invokeLater:
SwingUtilities.invokeLater(() -> {
var job = PrinterJob.getPrinterJob();
job.setJobName(report.getTitle());
job.setPageable(new PDFPageable(report.getDocument()));
if (job.printDialog()) {
job.print();
}
});

Related

Safari do not print Check Form properly where as Chrome does

When I print any Check Form, it opens as a PDF in the browser. It prints OK from Chrome but when I print it using Safari on MAC, it prints the same Check Form into two pages.
However, when I select the PDF or click on Check Form PDF and then print the Check Form using Safari, then it prints on single page, same like it prints from Chrome.
Any idea if there is any settings needs to be done in Safari for printing. Please suggest.
For printing always use PDF format if precise format matching is required:
On the workstation where it prints correctly, open the PDF and go into the print page setup then take note of all page settings, paper size, margins etc.. ask your technician to access page setup if you can't:
Be wary of any settings in page setup that attempts to resize the PDF before sending it to the printer:
Configure page setup (sometimes called print option) on Safari browser and macOS to match those that print successfully.
Links below are good starting points but actual configuration is dependent on OS and browser version. It's also out of scope of StackOverflow because it isn't a programming question. If you need help on configuring printers in Safari/MacOS and don't have a technician to do it, it would be better you ask on another stackexchange site, perhaps SuperUser would be the most appropriate:
https://support.apple.com/kb/ph25622?locale=en_US
https://support.apple.com/kb/PH21472?locale=en_US

How to save data from javafx form to pdf?

I am trying to implement a functionality such that:
When a user clicks on save button, whole data fetched from database by javafx (scene builder)form to get stored into the pdf form.
I have done with the snapshot, but I want the data obtained via text form to be stored in in pdf file.
Can someone please help me ?
give "DynamicReports" a try, it's easy to use, and i am using it to generate PDF file in my javafx projects. simple and powerfull.
http://www.dynamicreports.org/
Not only PDF file, it can also help to generates Csv, Docs, Excel, Html, Odt, Pptx, Rtx, Text...
You have a snapshot of your scene.
You can use the JavaFX Printing API, with PrinterJob.
The trick is that when your print anything, your computer's own print spooler will ask you for a destination.
My computer offers "Microsoft Print to PDF" and "Save to PDF".
So the "PDF" feature is trivially easy. All you have to do is, figure out how to use the API.

Printing a text file using wxWidgets

I found information about wxHtmlEasyPrinting & other printing features in wxWidgets, however I didn't find something as simple as I actually needed. All I want to do is print a text file
I don't want special formatting, I want a basic printout of basic text file containing say 10 lines of text.
I have a dialog. In this dialog I have a 'print' button. The print button should take you to a print setup page, from where one can get a print out of a text file. (So the print wouldn't be actually printing the dialogue, I dont necessarily need that)
Just to make it easier to understand:
void manual::OnButton1Click(wxCommandEvent& event)
{
PrintDialog1->ShowModal();
}
Right now the above code simply shows a print setup page, but obviously, clicking on print gets you nowhere.
Any idea where to start?
Printing even a plain text file is not that simple, you still need to wrap its lines, paginate it and so on, so you do need your own wxPrintOut implementation. You can look at the printing sample included with wxWidgets to see how to do it. Or you can indeed use wxHtmlEasyPrinting after trivial conversion of the text to HTML (basically you just need to escape the special XML entities).
Another solution is to use an external program for printing, which is the absolutely simplest way to do it, but doesn't provide any UI for the user at all.

characters missing when printing

We have a WPF application which can perform either a report preview or a report print.
Both requests use the same code.
Call the report service which gets the report from Microsoft Report Services.
Convert the report into the desired format (in this case PDF).
Then return the report as a byte array.
The result is then written to a temporary file as a binary stream, and either popped into a window to preview or start a Process to print.
In both cases the temporary file is passed.
Print Preview works flawlessly! But Print Report will print with all occurances of 'ti' disappearing. I see there is a printer escape sequence of ESC t NUL/SOH and I assume that if, for some reason, an escape character gets into that stream that ti will result in an ignored print sequence. Thus the missing characters.
My first question is if anyone has ever experienced this with generated PDF reports?
My second question (obviously) is if anyone knows of a utility I can use to view the binary data in the file being printed, to see what is in the file just before every 'ti' sequence?
After a great deal of searching I came across a post on the Adobe forum that states that version 8 had a bug where it was not printing character combinations. Once I dug deeper it seems that it has returned and the suggested workaround fixed our issue.
Workaround: Do a print as image.
Adobe seems to be unable to do the most basic of what their software must do, print the exact content!
Answer for your second question:
First, do one of the following two things:
Set the Windows print spooler properties to not delete printed jobs.
Pause the target print queue.
Then, grab the spool file from the Windows printspool directory (which location that is you can find out by looking at the (right-click) 'Properties...' dialog of the 'Printers and Faxes' folder).
I realize this is an old post but I wanted to add some updated info from the above comment stating that it's a problem with Acrobat 8. We are using Acrobat 10.1.6 and still have the same problem. From what I've read, it's a problem with the adobe product itself. The only real fix I've seen (actually work around) is to print as an image. LAME
Surprisingly this bug is still there in 2021. Adobe cannot be relied upon printing documents properly. This takes away all the allure of features it had if it cannot do the most basic stuff it is required for.
Printing as image reduces the quality and blur the document.
Simply open the document with Safari or Chrome and print from there. E
I had a similar problem while printing directly from the firefox (acrobat reader within). I downloaded the file and then printed. The problem was solved.

Exported PDFs from Mathematica 8 won't print

UPDATE: I wrote to Wolfram support about this and will update the post if they can resolve the problem. Sorry for spamming SO with a technical support question, but here it remains in case anyone else is having the same issue.
Is anyone else having this problem with Mathematica 8? I recently upgraded and noticed that when I export Graphics to a PDF file, although the file appears fine on my computer, it prints as a blank page. For example, try
Rectangle[{1,1}]//
Graphics//
Export["~/test.pdf",#]&
which creates a PDF file containing a black square. This file opens fine, but if I send it to my department printer I just get a blank page. If I don't export the graphics but print the notebook from MM, no problem, the graphics print as expected. If I use MM 7 to do exactly the same thing, the PDF file prints as expected. Exporting to PNG in MM8 seems to work fine. And, using the context menu Save Graphics As ... or File > Save Selection As ... to create a PDF containing just the graphic also works. However, these graphics eventually get included in a TeX document, and it would be far better if I could continue using the script I've got that doesn't require any button clicking to generate them.
I'm running MM 8.0.0.0 on Mac OS 10.6.7. I have not been able to test this on another printer yet, but this printer has never given me problems before and prints other PDF documents fine. Any ideas why this is happening?
Wolfram Research responds:
...
This issue has been reported by other users as
well and our developers are currently looking into it. I have added your
details to the report so you can be notified when this is resolved.
In the meantime, the alternatives that you could try are:
Try a different printer.
Rasterize the image with the function 'Rasterize' before exporting. If
the rasterized image loses some resolution, you could use the option
'ImageResolution' to edit this.
Rasterize[image, ImageResolution -> xxx]
Surely this is a bug (please report it to support#wolfram.com), but you can work around the problem by selecting the graphic and choosing File > Save Selection As... from the menu (or Save Graphic As... from the contextual menu). This produces a slightly different file that doesn't appear to exhibit the undesirable behavior we observe from Export[].
These problematic files, and LaTeX PDFs that include them, can be properly printed by Adobe Reader 10.1.2. That's if you're okay with installing and using a 450MB PDF reader.
I reproduced the problem (leading me to this question) with Mathematica 8.0.4.0 on Mac OS X 10.7.2. Wolfram suggested lame workarounds like Rasterize and told me
This issue has been addressed by our developers, and a fix will be included in a future version of Mathematica.