SurveyPDF save options
Basics
SurveyPDF exposes the following two methods - save()
and raw()
- that allow you to save a survey's generated PDF document to different formats, such as a downloadable PDF file, a JavaScript String or Blob object, etc.
save(fileName: string = 'survey_result.pdf'): Promise<any>
Docs | Sources
Downloads a PDF document as a file.
The fileName parameter specifies the name of the result PDF file. If omitted, defaults to "survey_result.pdf".raw(type?: string): Promise<string>
Docs | Sources
Returns a PDF document in different formats depending upon the value passed trough the type parameter. If the type parameter is undefined, theraw
method's output is a raw body of the resulting PDF returned as a string.
The type parameter accepts the following values:undefined
Allows you to obtain the PDF document as a JavaScript String object."dataurlstring"
Allows you to get the PDF document as a data URL string encoded into base64 format."blob"
Allows you to obtain the PDF document as a JavaScript Blob object."bloburl"
Allows you to get the PDF document as a URL to a Blob object.Note that the
raw
method wraps the jsPDF library's output method, so theraw
method might accept additional output types available foroutput
.
The sections below describe the available save options in more detail.
- Download a file
- Get a string object
- Get a data URL to a base64-encoded string
- Get a Blob object
- Get a URL to a Blob object
Download a file
Call the save method with an optional filename parameter to download a PDF file within a browser.
Syntax:
save(fileName: string = 'survey_result.pdf'): Promise<any>
Usage:
surveyPDF.save("mySurveyAsPDFFile");
This is asynchronous method.
If you do not specify the fileName parameter's value explicitly, the method uses the default "survey_result.pdf" file name.
const surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF.save("myPdfDocument.pdf");
See a Plunker sample:
SurveyPDF - How to save the result PDF as a file
Get a string object
Call the raw method without a parameter to get a PDF document as a String object. Note that JavaScript strings cannot represent all possible byte combinations of PDF documents, so the final PDF might be corrupted (e.g. it might miss a bold font or there might take place a font mismatch).
Syntax:
raw(type?: string): Promise<string>
Usage:
surveyPDF.raw();
This is asynchronous method.
Example:
Obtains a PDF document as a string, creates a Blob object from the string, creates a URL for the Blob object and downloads the Blob through a dynamically created a
element with href
set to the Blob URL.
Note: use only with default jspdf fonts.
var surveyPDF = new SurveyPDF.SurveyPDF(json, { fontName: "helvetica" });
surveyPDF.data = survey.data;
surveyPDF
.raw()
.then(function (text) {
var file = new Blob([text], {type: "application/pdf"});
var a = document.createElement("a");
a.href = URL.createObjectURL(file);
a.download = "surveyThroughString.pdf";
a.click();
URL.revokeObjectURL(a.href);
});
See a Plunker sample:
SurveyPDF - How to save the result PDF as a string
Get a data URL to a base64-encoded string
Call the raw method with the "dataurlstring" value passed as the type parameter to get a base64-encoded data URL string for the PDF document.
Syntax:
raw(type?: string): Promise<string>
Usage:
surveyPDF.raw('dataurlstring');
This is asynchronous method.
Use a data URL string to download a PDF
You can use the "dataurlstring" value in the raw
method's type parameter to download the generated PDF document.
Example:
Obtains a PDF document as a data URL string and assigns this URL to a dynamically created a
element's href
to download the PDF.
var surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF.data = survey.data;
surveyPDF
.raw('dataurlstring')
.then(function (text) {
var a = document.createElement("a");
a.href = text;
a.download = "surveyThroughDataUrlString.pdf";
a.click();
});
See a Plunker sample:
SurveyPDF - How to use a base64-encoded data URL string to save the result PDF
Use a data URL string to preview a PDF
You can use the "dataurlstring" value in the raw
method's type parameter to preview a PDF document within a web page (in the embed
html tag).
Example:
Obtains a PDF document as a data URL string, creates an embed
element, and assigns the data URL to the embed element's scr
to preview the PDF.
var surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF
.raw("dataurlstring")
.then(function (dataurl) {
var pdfEmbed = document.createElement("embed");
pdfEmbed.setAttribute("type", "application/pdf");
pdfEmbed.setAttribute("style", "width:100%");
pdfEmbed.setAttribute("height", 600);
pdfEmbed.setAttribute("src", dataurl);
var previewDiv = document.getElementById("pdf-preview");
previewDiv.appendChild(pdfEmbed);
});
See a Plunker sample:
SurveyPDF - How to use a base64-encoded data URL string to preview the result PDF
Get a Blob object
Call the raw method with the "blob" value passed as the type parameter to get a Blob object for the PDF document.
Example:
Obtains a PDF document as a Blob object, creates a URL for the Blob object, and downloads the PDF through a dynamically created a
element with href
set to the Blob URL.
var surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF
.raw("blob")
.then(function (blob) {
var a = document.createElement("a");
a.download = "surveyThroughBlob.pdf";
a.href = URL.createObjectURL(blob);
a.click();
URL.revokeObjectURL(a.href);
});
See a Plunker sample:
SurveyPDF - How to save the result PDF as a Blob
Get a URL to a Blob object
Call the raw method with the "bloburl" value passed as the type parameter to get a URL to a Blob object that represents the generated PDF document.
Example:
Obtains a PDF document as a URL to a Blob object and downloads the PDF through a dynamically created a
element with href
set to the Blob URL.
var surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF
.raw("bloburl")
.then(function (bloburl) {
var a = document.createElement("a");
a.href = bloburl;
a.download = "surveyThroughBlobUrl.pdf";
a.click();
});
See a Plunker sample:
SurveyPDF - How to save the result PDF through a Blob URL