Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert HTML (containing JavaScript ) to a PDF. How can I do that?
I just want to show what is being shown in web page. I am displaying a gantt chart that is generated by a JavaScript library.
Now I want to save that HTML web page as a PDF, how to do that?
We are also looking for some way to convert html files with complex javascript to pdf.
The javasript in our files contains document.write and DOM manipulation.
We have tried using a combination of HtmlUnit to parse the files and Flying Saucer to render to pdf but the results are not satisfactory enough. It works, but in our case the pdf is not close enough to what the user wants.
If you want to try this out, here is a code snippet to convert a local html file to pdf.
URL url = new File("test.html").toURI().toURL();
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(url);
OutputStream os = null;
try{
os = new FileOutputStream("test.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(page,url.toString());
renderer.layout();
renderer.createPDF(os);
} finally{
if(os != null) os.close();
}
I'm surprised no one mentioned the possibility to use an API to do the work.
Granted, if you want to stay secure, converting HTML to PDF directly from within the browser using javascript is not a good idea.
But here's what you can do:
When your user hit the "Print" (for example) button, you:
Send a request to your server at a specific endpoint with details about what to convert (URL of the page for instance).
This endpoint will then send the data to convert to an API, and will receive the PDF in response
which it will return to your user.
For a user point of view, they will receive a PDF by clicking on a button.
There are many available API that does the job, some better than others (that's not why I'm here) and a Google search will give you a lot of answers.
Depending on what is written your backend, you might be interested in PDFShift (Truth: I work there).
They offer ready to work packages for PHP, Python and Node.js. All you have to do is install the package, create an account, indicate your API key and you are all set!
The advantage of the API is that they work well in all languages. All you have to do is a request (generally POST) containing the data you want to be converted and get a PDF back. And depending on your usage, it's generally free, except if you are a heavy user.
Using the browser's Print... menu item, you can utilize a PDF Printer Driver, like PDFCreator. This way any JavaScript included in the page is processed by the browser when the page is rendered.
PDFCreator is a free tool to create PDF files from nearly any Windows application.
Create PDFs from any program that is able to print
With Docmosis or JODReports you could feed your HTML and Javascript to the document render process which could produce PDF or doc or other formats. The conversion underneath is performed by OpenOffice so results will be dependent on the OpenOffice import filters. You can try manually by saving your web page to a file, then loading with OpenOffice - if that looks good enough, then these tools will be able to give you the same result as a PDF.
Check this out http://www.techumber.com/2015/04/html-to-pdf-conversion-using-javascript.html
Basically you need to use html2canvas and jspdf to make it work. First you will convert your dom to image and then you will use jspdf to create pdf with the images.
EDIT:
A short note on how it work.
We will use two libraries to make this job done. http://html2canvas.hertzen.com/ and
https://github.com/MrRio/jsPDF
First we will create a dom image by using html2canvas them we will use jspdf addImage method to add that image to pdf.
It seems simple but there are few bugs in jsPdf and html2cavas so you may need to change dom style temporarily.
Hope this helps.
Copy and paste this in your site to provide a link which will convert the page to a PDF page.
Convert To PDF
You can do it using a jquery,
Use this code to link the button...
$(document).ready(function() {
$("#button_id").click(function() {
window.print();
return false;
});
});
This link may be also helpful: jQuery Print HTML Pdf Page Options Link
Related
Hi onto my react app I need to see a document but I cannot upload it manually. I'll explain, the user if need, could upload a document and into another part of the app could see it.
I want to know if there's a possibility to organize something like
See document
So the user onClick can see the doc opened in another tab. I do some logic but I upload the document on my store after he can see it in the review mode.
So can I visualize it without charging it on localStorage or sessionStorage, should I use an external library?
It depends on the format of the document. if it's an image you can show it by encoding it to base64 for other types of documents I'm afraid you have to have it stored at some URL.
You don't need to upload it anywhere to open it, after the 'upload' (using the file chooser) you can store it in a variable and use it as you please (keep in mind that this is not always a good approach). However if you want to visualize\open it in the application, it depends on the file format and you might need to use an external library to interpret and present it
edit: add note in parenthesis
I'm trying to store HTML files in the browser for an app built on NW.js or Electron.
Long story short: I want to make something like Sublime Text using a WYSIWYG editor (I don't know exactly how it works, so I will make a guess)
Creating a new TAB, all content inside the #editor is store in
localStorage/IndexedDB/NeDB/PounchDB/LimvoDB/... as the user is
writing.
When the user needs to save the file, it stores the content in the
browser window, and then it creates the file.
If the file already exists, the localStorage content overwrites it.
All the magic must happen around the browser DB.
You might be wondering why I'm not using files directly, and it's because the first request: We don't know if the user will save the file, but we don't want to lose all the content if the app is closed.
Searching the web, I find that is bad practice to pass HTML content through JSON, but I can't think of any other solution.. I'd have to use encodeURI and decode when retrieve the data to the #editor or the file saved.
I'm using:
Electron
Angular
I don't know yet what DB should I choose
Digging around, I also saw the sync function in PounchDB -> CouchDB and it blew my mind away ─it's a function to synchronize offline and online data using the named DB's.
Is it possible to store .HTML files in PounchDB and then synchronize it with CouchDB?
Is all this bad practice?
How would you do a Notepad - Sublime Text or a «MS Word» editor using PounchDB, or NeDB, or LimvoDB using Electron/NW.js engine?
Ended up using PouchDB, which also happen to handle very well html-strings.
I am new to web dev and I have a text file that I created using C# to collect some data from a website. Now I want to use that data to make graphs or some way to show the info on a website. Is it possible to use I/O in javascript or what is my best option here? Thanks in advance.
You have several options at your disposal:
Use a server-side technology (like ASP.Net, Node.js etc) to load, parse and display the file contents as HTML
Put the file on a web server and use AJAX to load and parse it. As #Quantastical suggested in his comment, convert the file to JSON forma for easir handling in Javascript.
Have the original program save the file in HTML format instead of text, and serve that page. You could just serve the txt file as is, but the user experience would be horrible.
Probably option 1 makes the most sense, with a combination of 1 + 2 to achieve some dynamic behavior the most recommended.
If you are working in C# and ASP then one option is to render the html from the server without need for javascript.
In C# the System.IO namespace gives access to the File object.
String thetext = File.ReadAllText(fileName);
or
String[] thetextLines = File.ReadAllLines(fileName);
or
If you have JSON or Xml in the file then you can also read and deserialize into an object for easier use.
When you have the text you can create the ASP/HTML elements with the data. A crude example would be:
HtmlGenericControl label = new HtmlGenericControl("div");
label.InnerHTML = theText;
Page.Controls.Add(label);
There are also HTMLEncode and HTMLDecode methods if you need them.
Of course that is a really crude example of loading the text at server and then adding Html to the Asp Page. Your question doesn't say where you want this processing to happen. Javascript might be better or a combination or C# and javascript.
Lastly to resolve a physical file path from a virtual path you can use HttpContext.Current.Server.MapPath(virtualPath). A physical path is required to use the File methods shown above.
I'm making a Windows store app (in Javascript) that generates a PDF. I convert this to base64 and then save that to file (if I want). This works fine.
(the PDF is a one page document (~30kb) with text, vector graphics and a small image)
Now I would like to be able to print this pdf directly from the app without having to open it in a separate application. Of course I've been doing a lot of searching, but the information I've come accross never seems to work. It either is in the wrong language, doesn't do what I'm looking for or just doesn't work. Also the Microsoft documentation is pretty vague and lacks decent examples.
Anyway, from what I've understood you can actually render a pdf page to bitmap and then send that to the printer. I decided to give it a try, so what I'm trying to do first is to save the pdf as an image to file.
Now I've managed to create a pdfPage object, now I'm supposed to do this:
pdfPage.renderToStreamAsync(outputStream).done( /* Your success and error handlers */ );
The outputStream is supposed to be a IRandomAccessStream object, but I can't seem to instance one. It doesn't show in the Streams list and when I type it in manually it doesn't work... Using InMemoryRandomAccessStream instead seems to work though.
var outputStream = new Windows.Storage.Streams.IRandomAccessStream(); //this don't work?
Even if outputStream is good, how do I save it to file? I've saved IBuffer's to file before, can I convert it to an IBuffer somehow? I can't find any information on that.
Also I believe it should be possible to show the outputStream as an image in the app. I can only find C# examples of this. How does this work in JS/HTML?
Okay I figured out how to save it:
Windows.Storage.ApplicationData.current.temporaryFolder.getFileAsync("mydocument.pdf").then(function (file) {
var pdfDocument = Windows.Data.Pdf.PdfDocument;
pdfDocument.loadFromFileAsync(file).then(function (pdf) {
page1 = pdf.getPage(0);
var accessStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
page1.renderToStreamAsync(accessStream).done(function () {
Windows.Storage.ApplicationData.current.temporaryFolder.createFileAsync("page1image.png", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (filestream) {
Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(accessStream.getInputStreamAt(0), filestream.getOutputStreamAt(0)).then(function () { console.log('done') });
});
});
});
});
});
Why do these seeminlgy straightforward things have to be so complicated?
The whole rasterization doesn't work too well. I mean to get a decent resolution (600dpi) the file takes some time to generate. I noticed that even the printer needs a couple of breaks while printing to keep up. This doesn't happen at all when I print the pdf directly. Also you lose the CMYK definition.
But what my real concern is: when I print an image it always adds 2cm margins to the page. The pdf image has margins of its own already so now it's double. 2cm is way too much anyway. I can't find any settings anywhere where I can change this.
So: does anyone know how to change the margins when printing from a Windows store app?
I would like to save a csv file from a web page. However, the link on the page
does not lead directly to the file, but it calls some kind of javascript, which leads
to the opening of the file. In other words, there is no explicit url address for the
file i want to download or at least I don't know what it should be.
I found a way to download a file by activating Internet Explorer,going to the web page
and pressing the link button and then saving the file through the dialog box.
This is pretty ugly, and I am wondering if there is a more elegant (and fast) method to retrieve a file without using internet explorer(e.g. by using urllib.retrieve method)
The javascript is of the following form (see the comment, it does not let publish the source code...):
"CSV"
Any ideas?
Sasha
You can look at what the javascript function is doing, and it should tell you exactly where it's downloading from.
I had exactly this sort of problem a year or two back; I ended up installing the rhino javascript engine; grepping the javascript out of the target document and evaluating the url within rhino, and then fetching the result.