I wrote an application to create PDF file to PDDocument file it work fine. i use the pdfbox library
PDDocument pdfDoc = PDDocument.load(pdfFile);
Now i want to create PS(Post script) file from PDF file. Is there are any way in java. I can use any free API.
Many thanks.
Adobe seems to have a library. Here are some instructions. Please note, I have not tried this myself: http://help.adobe.com/en_US/livecycle/9.0/programLC/help/index.htm?content=000761.html
This link has a more detailed solution:
http://help.adobe.com/en_US/livecycle/9.0/programLC/help/index.htm?content=000074.html
You can use PDFDocument to load your PDF then use PSConverter to convert the PDF document into an OutputStream.
The library I'm using is called ghost4j:
import org.ghost4j.converter.PSConverter;
import org.ghost4j.document.PDFDocument;
Here's a small snippet:
private ByteArrayOutputStream convertPDFtoPS(){
ByteArrayOutputStream outstreamFile = new ByteArrayOutputStream();
try{
PDFDocument document = new PDFDocument();
//getPDFFile just returns an InputStream of the PDF file
document.load(getPDFFile());
PSConverter converter = new PSConverter();
converter.convert(document, outstreamFile);
outstreamFile.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return outstreamFile;
}
Related
How can I transform a PDF with forms made in Adobe Livecycle to a simple image PDF using Java?
I tried using Apache PDFBox but it can't save as image a PDF with forms.
This is what I tried(from this question: Convert PDF files to images with PDFBox)
String pdfFilename = "PDForm_1601661791_587488.pdf";
try (PDDocument document = PDDocument.load(new File(pdfFilename))) {
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
ImageIOUtil.writeImage(bim, pdfFilename + "-" + (page+1) + ".png", 300);
}
} catch (IOException ex) {
Logger.getLogger(StartClass.class.getName()).log(Level.SEVERE, null, ex);
}
But is not working, the result is an image where it writes that "The document you are trying to load requires Adobe Reader 8 or higher.
I guess is not possible, I tried many libraries and none worked.
This is how I solved the problem:
I used an external tool - PDFCreator.
In PDFCreator I created a special printer that prints and saves the PDF without asking any questions(you have these options in PDFCreator).
This is simple to reproduce in PDFCreator because in the Debug section you have an option to load a config file, so I have this file prepared, I just install PDFCreator and load the config file.
If you will use my INI file in the link above you should know that the resulted PDF is automatically saved in the folder: "current user folder/Desktop/temporary".
The rest of the job is done from Java using Adobe Reader, the code is in my case:
ProcessBuilder pb = new ProcessBuilder(adobePath, "/t", path+"/"+filename, printerName);
Process p = pb.start();
This code opens my PDF in AdobeReader, prints the PDF to the specified virtual printer, and exists automatically.
"adobePath" is the path to the adobe executable
path+"/"+filename is the path to my PDF.
"printerName" is the name of the virtual printer created in PDFCreator
So this is not a pure Java solution and in the future, I intend to use Apache PDFBox to generate my PDF's in a format that is compatible with browsers and all readers...but this works also.
I'm using either pdfbox-app-2.0.18.jar or pdfbox-app-2.0.17.jar.
From the example here, I have this code below :
try (FileOutputStream fos = new FileOutputStream(signedFile);
PDDocument doc = Loader.loadPDF(inputFile)) {
// code
}
After executing this code, I'm getting this error given below :
org.apache.pdfbox.Loader is not found
How to resolve this issue ?
Loader class is never introduced in version 2.x or lower. So, you can't use it.
Alternatively, you can use load() method from PDDocument class to load PDF files.
Modify to this :
try (FileOutputStream fos = new FileOutputStream(signedFile);
PDDocument document = PDDocument.load(inputFile)) {
// code
}
Read this :- https://pdfbox.apache.org/2.0/migration.html
The Loader class has been added January 25, 2020. SVN log
It's not part of version 2.0.18, as it is not in this file:
pdfbox-2.0.18-src.zip
So this class is simply too new and that's why you cannot use it!
The PDDocument class will represent the PDF document that is being processed. Its load() method will load in the PDF file specified by the File object :
PDDocument document = PDDocument.load(new File("path/to/pdf"));
I am trying to convert a docx document containing a logo to a pdf document.
I have tried this :
FileInputStream in=new FileInputStream(fileInput);
XWPFDocument document=new XWPFDocument(in);
File outFile=new File(fileOutput);
OutputStream out=new FileOutputStream(outFile);
PdfOptions options=null;
PdfConverter.getInstance().convert(document,out,options);
But in the pdf document the logo is not at the right place.
Is there a way to create a PDF that is exactly the same as the docx document?
Could be document4j an option? It delegates the convertion to the native application.
This is achieved by delegating the conversion to any native application which understands the conversion of the given file into the desired target format.
File wordFile = new File( ... );
File target = new File( ... );
IConverter converter = ... ;
Future<Boolean> conversion = converter
.convert(wordFile).as(DocumentType.MS_WORD)
.to(target).as(DocumentType.PDF)
.prioritizeWith(1000) // optional
.schedule();
You can quickly test if the convertion fit your requirement with the "Local demo" on a Windows machine with Word and Excel installed:
git clone https://github.com/documents4j/documents4j.git
cd documents4j
cd documents4j-local-demo
mvn jetty:run
Then go to http://localhost:8080
See the full documentation here :
http://documents4j.com/#/
I am using org.apache.poi.xwpf.converter.xhtml.XHTMLConverter class to convert docx to html. Below is my groovy code
public Map convert(String wordDocPath, String htmlPath,
Map conversionParams)
{
log.info("Converting word file "+wordDocPath)
try
{
...
String notificationWorkingFolder = "C:\tomcats\Notification\store\Notification1234"
FileInputStream fis = new FileInputStream(wordDocPath);
XWPFDocument document = new XWPFDocument(fis);
XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File(notificationWorkingFolder)));
File htmlFile = new File(htmlPath);
OutputStream out = new FileOutputStream(htmlFile)
XHTMLConverter.getInstance().convert(document, out, options);
log.info("Converted to HTML file "+htmlPath)
return [success:true,htmlFileName:getFileName(htmlPath)]
}
catch(Exception e)
{
log.error("Exception :"+e.getMessage(),e)
return [success:false]
}
}
The above code is converting docx to html successfully, but if docx contains any images it puts <img src="C:\tomcats\Notification\store\Notification1234\word\media\image1.png"> but do not copy the image to that folder. As a result, when I open html tag, all images appears empty. Am I missing something in code? Is there a way to generate an image srouce link instead of absolute path, like <img src="http://localhost:8080/webapp/image1.png">
I got answer for first question from this link lychaox.com/java/poi/Word07toHtml.html. I had to add one line of code options.setExtractor(new FileImageExtractor(imageFolderFile)); to generate images.
Second question I resolved by pattern search and replacement.
Even with proper usage, it's worth noting that XHTMLConverter uses XHTMLMapper, which does not process headers, footers, or VML Images. Any images falling into those categories will be lost.
The PDFConverter is more fully featured, but also uses the GPL licensed library, iText.
I need to upload a PDF file to users google docs which is generated by pdfjet on google app engine. I figure out to generate pdf using pdfjet for gae/j. pdfjet uses streams to create the pdf. Is there anyway to convert stream to file so I can upload to users google docs. I tried gaevfs but couldn't make it work. I can use another pdf generation solution if needed or another virtual file system etc.
PDF generation code
ByteArrayOutputStream os = new ByteArrayOutputStream();
PDF pdf = new PDF(os);
Google Docs API code
DocumentListEntry newEntry = new PdfEntry();
newEntry.setTitle(new PlainTextConstruct("Some Report"));
The line I couldn't get make it work : setFile(File, String)
newEntry.setFile(pdf, "application/pdf");
Thanks.
Couldn't you simply write from ByteArrayOutputStream to FileOutputStream using the ByteArrayOutputStream.writeTo(OutputStream) method? Like this:
ByteArrayOutputStream os = new ByteArrayOutputStream();
PDF pdf = new PDF(os);
FileOutputStream fos = new FileOutputStream("myPDF.pdf");
baos.writeTo(os);