Javascript Writing to PDF or Excel - html

Hey Related to your answer on Can I write files with HTML5/JS?
How would I modify this for say Excel or PDF. I tried but was unable to get it to work. I get a corrupt file downloaded when I change MIME to application/pdf
I am trying to link it to data stored in localstorage which I have all in a variable.
My current code is:
function setSaveFile(contents, file_name, mime_type) {
var a = document.getElementById('save');
mime_type = mime_type || 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // text/html, image/png, et c
if (file_name) a.setAttribute('Log.xls', file_name);
a.href = 'data:'+ mime_type +';base64,'+ btoa(contents || 'Description' + ' ' + 'Notes' + ' ' + 'Date\n\n' + pdftimeLog);
}
I want to be able to export it as a CSV with each of those headings: DATE, description, notes in different cells. I want it mobile friendly.Thanks

Excellent Options for CSV Everyone:
http://snapshotmedia.co.uk/blog/jspdf
Is it possible to use any HTML5 fanciness to export local storage to Excel?
http://css-tricks.com/localstorage-examples/
Local Storage manipulation and possible sending
Thanks!

For a file to be a valid pdf or excelfile their contents need to conform to the standard that defines the respective format, just changing mimetypes won't do much good.
For PDF there's a library jspdf that might work for you.
I haven't seen a solution for Excel yet, it will be a whole lot harder as the (xls) file format is quite complicated, for the time being it's probably preferable to generate the file on the server.

As an alternative to CSV, it's also possible to write an Excel file by writing HTML to a file with a .xls extension. Excel opens it up based on the extension. Haven't yet explored how deeply it can paginate, format.

Related

Make tabular Data downloadable as a CSV file

I have a website which offers data in standard HTML table form and is displayed across 7 columns. At the backend, data is stored in MySQL and displayed through PHP on the webpage.
Site visitors have been demanding that data be made available for download in a CSV file, and I am OK to offer that feature.
What is the best way to offer a limited number of columns (say 3 out of 7 columns) with a one-click download into CSV file?
Note: this question is NOT about site visitors scraping data using python or other scripts, but it is about a webmaster willingly offering the option to download data in a CSV file.
Searched for and checked this thread XML or CSV for "Tabular Data", but it does not have a precise answer, hence the question.
Create a new PHP file (page) that instead of rendering the content as HTML will render it as a CSV. Then manipulate response headers to make clear to the browser that it is a file meant to be downloaded. This is how your .php file should look like:
<?php
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="file.csv"');
// ... QUERY FOR DATA
$outstream = fopen("php://output", 'w');
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals, ',', '"');
}
// CSV header
__outputCSV(['header1', 'header2', ...]);
// CSV body
array_walk($data, '__outputCSV', $outstream);
fclose($outstream);
?>

Write Full HTML Elements in JSON and onto screen [duplicate]

So what I'm trying to do is get text from a file in the same directory as my html file using JavaScript. I want to store an array inside a text file and change it whenever i want instead of constantly having to go into the code, save it, check if it works etc.
I've tried looking around but couldn't find any clear information, most of what I found is using .readAsBinaryString, etc..
I'm mostly seeing things like this but i can't seem find anything which is actually getting information from a textfile without making the person find the text file directory.
function storearray(newval){
var file = "file location;"
var txt = file.txt;
var array = txt.split("|");
txt = txt + newval + " | ";
return array;
}
To read a file from the user's disk you need to use FileReader and the user must explicitly select the file using a file input. (See JavaScript read file without using input).
To read a a file from the website you need to use Ajax (with fetch, XMLHttpRequest or a library that wraps around them like Axios). (See Using fetch from MDN).
If (as it seems here) you want to read data from the website but the website exists only on the user's disk then you still need to use Ajax but will usually run into security restrictions. Some browsers allow you to disable the security protection, but the general solution is to install a web server and load both HTML and the data file using HTTP.
Alternatively, you can store your data in JavaScript (you are generating an array from your text file, you can so that manually or have a build-time script do it) and just load it with a <script> element.

How to attach documents in ms access using vb6

Good day. I have an Access database with a field using an attachment data type. Is there a way to use Visual Basic 6 to attach my documents, such as images, Word files and PDF files on that database? Specifically on the field using the attachment data type.
Yes, from the LoadFromFile Method.
Snippet below:
Set AttachmentRecordset = RecordSetForTable.Fields("AttachmentFieldName").Value
FileName = "SomePathToAFile"
AttachmentRecordset.AddNew
AttachmentRecordset.Fields("FileData").LoadFromFile (FileName)
AttachmentRecordset.Update

Attempting to Attach a CSV, Converting to PDF

I have a code where I am attempting to attach a CSV saved in Drive (automatically replaced) to an email. At the moment, I can get it to add using fileBlob - DriveApp.getFileById(...).getBlob() but, it's attaching as a PDF.
Per Mogsdad's answer on this question there is a way to attach a CSV, but looking through the documentation I can't seem to find anything that will allow using a CSV. In fact, according to the documentation for getAS() it literally says: "For most blobs, 'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif', 'image/jpeg', or 'image/png' are also valid."
Does this mean in is impossible to attach a CSV (or, really, anything other than PDF) from Drive to an email using Apps Scripts?
Assuming the CSV file is in your Drive, you can use this snippet to attach it to an email.
function sendCSV() {
var blob = DriveApp.getFileById(FILE_ID).getBlob();
MailApp.sendEmail("email#example.com", "Subject", "CSV", {attachments:[blob]});
}
As per following Google Documentation: -
https://developers.google.com/apps-script/reference/base/blob-source.html
Following is mentioned: -
For most blobs, 'application/pdf' is the only valid option.
Following expected work-around: -
https://gist.github.com/nirajkadam/0b41a01b8e739800c964

Need to convert html to google doc native format using Java

I've read through the API documentation at https://developers.google.com/drive/v2/reference/ however I cannot find the answer to my question. And attempts to google a solution have failed.
I have a series of previously uploaded small HTML files sitting in Google Drive. What I want to do is write a short application to convert each of these to native Google Document format (mime type "application/vnd.google-apps.document").
I want to do this using Java code and not using GAS code.
The approach I used was to query drive for the File object corresponding to the item I want to convert. Then I pull that file's content as a string. Then I create a new file of mime type "application/vnd.google-apps.document" and upload it with the HTML content. Not surprisingly it didn't work.
So then I tried a different approach: Upload the content as "text/html" but set the "convert" flag to "true". Well I didn't see any direct API to set the convert flag to true. So I tried:
File oBody = new File() ;
oBody.setTitle ( sTitle ) ;
oBody.setDescription ( sDescription ) ;
oBody.setMimeType ( sMimeType ) ;
oBody.set("convert", bConvert);
This did not fail. But it did not create a Google Document either. It just created a text file identical to the original file.
How do I upload a document containing "text/html" content and get Google Drive to convert it automatically to a Google Document?
The convert flag has to be set in the files.insert request and not the File resource.
Using the snippet in the files.insert documentation as reference, this is what you should do:
...
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
...