Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed basic.html and updated 'pdfjsnewwindow' #2855

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/PDF.js/web/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4886,10 +4886,11 @@
}

function getPDFFileNameFromURL(url) {
var defaultFilename =
arguments.length > 1 && arguments[1] !== undefined
? arguments[1]
: "document.pdf";
let downloadName = (window.location.search).split('=')[2];
var defaultFilename = downloadName || "document.pdf";
// arguments.length > 1 && arguments[1] !== undefined
// ? arguments[1]
// : "document.pdf";

if (typeof url !== "string") {
return defaultFilename;
Expand Down
4 changes: 3 additions & 1 deletion examples/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

<script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript" src="../dist/jspdf.debug.js"></script>
<script type="text/javascript" src="../dist/jspdf.umd.js"></script>
<script type="text/javascript" src="js/basic.js"></script>

<script>
window.jsPDF = window.jspdf.jsPDF;

$(function () {
$("#accordion-basic, #accordion-text, #accordion-graphic, #accordion-font").accordion({
autoHeight: false,
Expand Down
3 changes: 2 additions & 1 deletion src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ function jsPDF(options) {
"<style>html, body { padding: 0; margin: 0; } iframe { width: 100%; height: 100%; border: 0;} </style>" +
'<body><iframe id="pdfViewer" src="' +
pdfJsUrl +
'?file=" width="500px" height="400px" />' +
'?file=&downloadName=' + options.filename + '" width="500px" height="400px" />' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arguments seem a little messed up now...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only way so far that I can think of to pass the file name for download using pdf.js. Currently, it defaults to "ducment.pdf". The arguments in viewer.js seem always has only one element - the blob URL, so it's not useful for that matter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's alright. It's just that the URL parameters now look like this

?file=&downloadName=filename.pdf

which looks a bit broken. Do you know what the "file=" part does? Seems to me like we can remove that part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually file= is required, it provides the PDF file for rendering. Normally it would be ?file=/folder/test.pdf. Here it is blank and I think the pdf content is populated by bloburl instead. I tried to remove it, but ?downloadName=filename.pdf returns error:
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. Didn't test it. Then everything is fine ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just for the record, it passes the bloburl as the file name to pdf.js first, then replaces it with a physical pdf file name/path if exists. How the bloburl is inserted as the file name is not clear to me yet.

"</body></html>";
var PDFjsNewWindow = globalObject.open();

Expand All @@ -3036,6 +3036,7 @@ function jsPDF(options) {
PDFjsNewWindow.document.documentElement.querySelector(
"#pdfViewer"
).onload = function() {
PDFjsNewWindow.document.title = options.filename;
PDFjsNewWindow.document.documentElement
.querySelector("#pdfViewer")
.contentWindow.PDFViewerApplication.open(
Expand Down