PDFJET Does Not Work For En Empty Constructor - java

i want to use pdfjet for a Google app engine project.
i downloaded the Java jar from the pPdfjet home page.
i followed an example given in a stack-overflow example and the examples given in the home page.
all the examples uses an empty constructor: PDF pdf=new PDF();. However when i try to use it,
it says that the constructor PDF() is undefined, further more all the method shown do not work:
pdf.wrap(): is undefined
pdf.save("Example_03.pdf"): is undefined

It looks like the examples on their web page are out of date. Look at the examples in the zip download instead. This simple example works for me:
OutputStream out = new FileOutputStream("test.pdf");
PDF pdf = new PDF(out);
Page page = new Page(pdf, Letter.PORTRAIT);
pdf.flush();
out.close();

Ok this is easy. Actually instead of taking from req.getOutputStream() directly create and instance of BytArrayOutputStream and use that.
For sending it just use out.toArray() as add it to the attacement part.

Related

HtmlUnit HtmlFileInput.setData() not working

I am trying to upload a file to a website using the HtmlUnit HtmlFileInput class. I have the data in a byte[] array and would like to send it up without writing it to a file first.
I'm trying:
HtmlFileInput fileInput = form.getInputByName("file");
fileInput.setData(data);
HtmlElement button = form.getInputByName("validate");
HtmlPage responsePage = button.click();
This is not working. But, when I try
HtmlFileInput fileInput = form.getInputByName("file");
fileInput.setValueAttribute("file.txt");
HtmlElement button = form.getInputByName("validate");
HtmlPage responsePage = button.click();
Everything works fine. The docs seem to indicate that setData() does exactly what I want to do, but it doesn't seem like any of the HtmlUnit code even uses the data_ variable that is set when setData() is called. The code uses the files_ field which is set when setValueAttribute() is called.
I noticed several old bugs that were opened that talked about similar problems and it says that they were all fixed.
Am I trying to use setData() in a way that it shouldn't be used?
Thanks.
In short - data_ is used by getSubmitNameValuePairs() and there are also unit tests for that (e.g. com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.setValueAttributeAndSetDataDummyFile()).
The trick here is the missing rest of the file - you have to simulate a bit more if you like to get your stuff uploaded. Please set the Value (to submit a dummy file name) and the content type also to help the server to understand your data.
HtmlFileInput fileInput = form.getInputByName("file");
fileInput.setValueAttribute("dummy.txt");
fileInput.setContentType("text/csv");
fileInput.setData("My file data".getBytes());
I think i have to improve the documentation for this a bit.
If you like we can discuss this or if you like to see a quick fix - simply open an issue on github.

How to Retrieve the Download Link for Large Video Files

I would like to retrieve the download link for large video files. I have no problems with small video files but with large videos, the response from the server is that the file
"exceeds the maximum file size that Google can scan"
I want to use the link as the source to a video tag. But because that link gives me the error, I can't use it.
I'm using the Java SDK and I'm using File.getWebContentLink() to get the link. I've tried getDownloadLink() but that one doesn't even work.
Basically, is there anyway I can get the download link for large video files?
getWebContentLink() is designed for interactive users (browsers).
Instead, at the raw API level you'll want to use File.get with alt=Media AND also set the acknowledgeAbuse flag if you initially get returned the 'Google can't scan'. Read more on downloading files here and the abuse flag here.
In the Java client library, it'd look something like this:
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.set("acknowledgeAbuse", true)
.executeMediaAndDownloadTo(outputStream);
Disclaimer, I haven't compiled the above.
Note: Do not use the .../host/id method mentioned in the other answer - that method is deprecated and scheduled to stop serving content by end of August, 2016 (this year)
Try using https://googledrive.com/host/id where id is the file's ID. Inspired by http://www.scriptscoop2.com/t/1eb5579419c6/issue-when-trying-to-stream-a-video-from-google-drive-inside-html5-vid.html.

can i insert js codes using out.write method in java

How can i insert javascript in java file. If i am inserting html tags it works fine but if i insert following js code it doesn't show any errors but it will not show the chart also.
i'am using chart.js
out.write(""<h1>Graph</h1>\n"");
out.write("<canvas id='canvas' height='450' width='600'></canvas>\n");
out.write("<script>\n");
out.write("var barChartData = \n");
out.write("{labels : ['Pass','Fail'],\n");
out.write("datasets : {\n");
out.write("[fillColor : 'rgba(220,220,220,0.5)',\n");
out.write("strokeColor : 'rgba(220,220,220,1)',\n");
out.write("data : [65,0]},{\n");
out.write("fillColor : 'rgba(151,187,205,0.5)',\n");
out.write("data : [0,47]}]}\n");
out.write("var myLine = new Chart(document.getElementById('canvas').getContext('2d')).Bar(barChartData);\n");
out.write("</script>\n");
Is this is the correct way using out.write ?
According to the HTML 4 specification, a <script> element requires either a type or lang attribute, unless you have specified the default scripting language.
A document that doesn't conform to this is erroneous, and the browser is free to ignore the script.
Another possibility is that your script contains errors. Use your browser's web developer support to check for javascript errors.
If you are writing to a file on Windows, use \r\n instead, because windows filesystem doesn't understands \ns as newlines. Else, you must provide more info. It could also be because you missed declarating the script type.

how to encapsulate a URL site into a Java object?

I hava a Java/Grails app that needs to "read" the contents of a given URL to use it as an image, mostly to be dinamically resized.
My app already parses the url into HTML code using an implementation based on this post: http://www.roseindia.net/java/example/java/io/SourceViewer.shtml.
The class I built returns a String object that contains the HTML source code. Now I want to write this String into an object similar to a BufferedImage so I can display the captured URL into my new application.
any ideas, thanks in advance!
You can use a service like Bluga.net WebThumb and use Glen Smith's ThumbnailService to interface with it.
Or, if you really want to do this by yourself, you can use his Thumbnail Server (with an older version of the ThumbnailService), that he used to use before migrating to WebThumb ;)
Regards
You can create an Image object from a url :
URL url = new URL("http://url.to/your/image.jpg";
Image image = Toolkit.getDefaultToolkit().createImage(url)
If by
take a 'print screen' of the this site
you mean display in your app take a look at this : http://www.java-tips.org/java-se-tips/javax.swing/how-to-display-pages-for-a-web-site-in-your-applic.html

how to create an odt file programmatically with java?

How can I create an odt (LibreOffice/OpenOffice Writer) file with Java programmatically? A "hello world" example will be sufficient. I looked at the OpenOffice website but the documentation wasn't clear.
Take a look at ODFDOM - the OpenDocument API
ODFDOM is a free OpenDocument Format
(ODF) library. Its purpose is to
provide an easy common way to create,
access and manipulate ODF files,
without requiring detailed knowledge
of the ODF specification. It is
designed to provide the ODF developer
community with an easy lightwork
programming API portable to any
object-oriented language.
The current reference implementation
is written in Java.
// Create a text document from a standard template (empty documents within the JAR)
OdfTextDocument odt = OdfTextDocument.newTextDocument();
// Append text to the end of the document.
odt.addText("This is my very first ODF test");
// Save document
odt.save("MyFilename.odt");
later
As of this writing (2016-02), we are told that these classes are deprecated... big time, and the OdfTextDocument API documentation tells you:
As of release 0.8.8, replaced by org.odftoolkit.simple.TextDocument in
Simple API.
This means you still include the same active .jar file in your project, simple-odf-0.8.1-incubating-jar-with-dependencies.jar, but you want to be unpacking the following .jar to get the documentation: simple-odf-0.8.1-incubating-javadoc.jar, rather than odfdom-java-0.8.10-incubating-javadoc.jar.
Incidentally, the documentation link downloads a bunch of jar files inside a .zip which says "0.6.1"... but most of the stuff inside appears to be more like 0.8.1. I have no idea why they say "as of 0.8.8" in the documentation for the "deprecated" classes: just about everything is already marked deprecated.
The equivalent simple code to the above is then:
odt_doc = org.odftoolkit.simple.TextDocument.newTextDocument()
para = odt_doc.getParagraphByIndex( 0, False )
para.appendTextContent( 'stuff and nonsense' )
odt_doc.save( 'mySpankingNewFile.odt' )
PS am using Jython, but the Java should be obvious.
I have not tried it, but using JOpenDocument may be an option. (It seems to be a pure Java library to generate OpenDocument files.)
A complement of previously given solutions would be JODReports, which allows creating office documents and reports in ODT format (from templates, composed using the LibreOffice/OpenOffice.org Writer word processor).
DocumentTemplateFactory templateFactory = new DocumentTemplateFactory();
DocumentTemplate template = templateFactory .getTemplate(new File("template.odt"));
Map data = new HashMap();
data.put("title", "Title of my doc");
data.put("picture", new RenderedImageSource(ImageIO.read(new File("/tmp/lena.png"))));
data.put("answer", "42");
//...
template.createDocument(data, new FileOutputStream("output.odt"));
Optionally the documents can then be converted to PDF, Word, RTF, etc. with JODConverter.
Edit/update
Here you can find a sample project using JODReports (with non-trivial formatting cases).
I have written a jruby DSL for programmatically manipulating ODF documents.
https://github.com/noah/ocelot
It's not strictly java, but it aims to be much simpler to use than the ODFDOM.
Creating a hello world document is as easy as:
% cat examples/hello.rb
include OCELOT
Text::create "hello" do
paragraph "Hello, world!"
end
There are a few more examples (including a spreadsheet example or two) here.
I have been searching for an answer about this question for myself. I am working on a project for generating documents with different formats and I was in a bad need for library to generate ODT files.
I finally can say the that ODFToolkit with the latest version of the simple-odf library is the answer for generating text documents.
You can find the the official page here :
Apache ODF Toolkit(Incubating) - Simple API
Here is a page to download version 0.8.1 (the latest version of Simple API) as I didn't find the latest version at the official page, only version 0.6.1
And here you can find Apache ODF Toolkit (incubating) cookbook
You can try using JasperReports to generate your reports, then export it to ODS. The nice thing about this approach is
you get broad support for all JasperReports output formats, e.g. PDF, XLS, HTML, etc.
Jasper Studio makes it easy to design your reports
The ODF Toolkit project (code hosted at Github) is the new home of the former ODFDOM project, which was until 2018-11-27 a Apache Incubator project.
the solution may be JODF Java API Independentsoft company.
For example, if we want to create an Open Document file using this Java API we could do the following:
import com.independentsoft.office.odf.Paragraph;
import com.independentsoft.office.odf.TextDocument;
public class Example {
public static void main(String[] args)
{
try
{
TextDocument doc = new TextDocument();
Paragraph p1 = new Paragraph();
p1.add("Hello World");
doc.getBody().add(p1);
doc.save("c:\\test\\output.odt", true);
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
There are also .NET solutions for this API.

Categories