Accessing or saving a PDF document for print directly from an iPhone using JavaScript

Is there a way to access or download a PDF file on an iPhone using JavaScript in a web application?

I have tried using window.open(src); and it works on Android and all desktop operating systems, but not on iPhone.

I need to be able to generate a PDF file from the server and either save it or print it directly from my mobile phone through the web application. Can anyone help me with this?

Thank you in advance! :)

Answer №1

To create a function that opens a print window with the provided innerHTML, you can use the code snippet below:

Code snippet:


openPrintWindow: function (printHtmlStr) {
        var me = this;

    var dt = new Date().getTime();

    var html = '<!DOCTYPE HTML>' +
    '<html>' +
    '<head>' +
    '<meta http-equiv="x-ua-compatible" content="IE=Edge"/>' +
    '<link rel="stylesheet" type="text/css" href="appRes/css/print.css?_dc=' + dt + '" media="all" />' +
    '</head>' +
    '<body>' +
    '<div class="print">' + printHtmlStr + '</div>';

    html += '</body>';
    html += '</html>';

    var iframe = this._printIframe;
    if (!this._printIframe) {
        iframe = this._printIframe = document.createElement('iframe');
        document.body.appendChild(iframe);

        iframe.style.display = 'none';
        iframe.onload = function () {
            setTimeout(function () {
                iframe.focus();
                iframe.contentWindow.print();
            }, 1);
        };
    }
    iframe.src = "about:blank";
    var doc = iframe.contentWindow.document;
    var windowUrl = window.location;
    var uniqueName = new Date();
    var windowName = 'Print' + uniqueName.getTime();

    this.testwindow = doc.open(windowUrl, windowName);

    doc.write(html);
    doc.close();

}

Feel free to adapt and use this function in your project for printing purposes.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The function to save a mongoose schema is not valid

I encountered an issue with the StripeToken.save function while using the code snippet below. After double-checking my model, I can't seem to pinpoint what went wrong. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var s ...

Click event triggered the function, however it was unable to modify the variable in $scope

<!doctype html> <html ng-app> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> <script src="script.js"></script> </head> <body> <ul c ...

"Php makes it easy to organize seating arrangements with drag-and-drop functionality

We are currently in the process of developing a website dedicated to bus services and booking tickets. Our main goal is to allow the administrator to easily create and modify seating arrangements through drag-and-drop functionality, including the ability t ...

Ray casting in Three.js used to target specific points

I am currently working on creating a network visualization using Three.js, and I have encountered an issue with my ray casting implementation. It seems that the points being identified by the ray caster are not the correct ones (Here's the full exampl ...

How can ReactJS continuously dispatch promises after a set interval?

Within my React component, I am invoking an action in ComponentDidMount() as shown below: componentDidMount() { const { actions } = this.props function save_project_continuously() { console.log("inside") actions.sa ...

What causes a 500 error when using PHP eval in conjunction with AJAX?

Working within a system where all PHP code resides in a database for dynamic alterations has presented me with an interesting challenge. While the code displays perfectly on the page, calling the same code via AJAX triggers a frustrating error 500. I' ...

What is the proper way to request confirmation before making updates or deletions to data?

my current situation is as follows: I am utilizing an el-table component to display data fetched from an API. I have organized this data into a computed property so that I can easily sort, filter, and paginate the table. Additionally, I have incorporated ...

What is the best way to indicate the region of a shape that intersects with another shape in

Picture yourself with an area and placing a camera lens or eye above it. What I'd like to do is create an eclipse on the area to show exactly what the lens or eye can see. So far, I have achieved this: https://i.sstatic.net/VHWc5.png You can interac ...

What is causing the delay in starting to play an audio track when it is clicked on?

I am facing an issue with my application and have created a minimum code example on StackBlitz to demonstrate the problem. The problematic code is also provided below. My goal is to have the Audio component play a track immediately when the user clicks on ...

Is it possible to transition to React without having a comprehensive understanding of JavaScript?

After building a foundation in JS, I am eager to delve into learning more about es6 and beyond. Would it be advisable for me to begin exploring React or Vue at this point? Since graduating recently, I have noticed that many companies value candidates who ...

The component is failing to detect that it is being exported

Encountering a strange error here. I exported the Home component in the usual way. See below for the relevant .js files to fix this error. However, the iOS simulator keeps showing the following error: Element type is invalid: expected a string (for built- ...

How to apply class binding within a v-for loop in Vue.js

When using Vuejs3, I am currently iterating through an array of objects: <div v-for="ligne in lignes" :key="ligne.id" :class="{ 'border-b-2':isSelected }" :id="`ligne_${ligne.id}`" > ...

When using the `display: table-cell` property on a div element, it does not automatically respect

How can I restrict the width of columns by defining a width attribute on the div.dcolumn DOM element to preserve the layout with overflowing cells? I want the content of any overflowing cell (like the 9px column) to be hidden while maintaining the specifie ...

Utilizing puppeteer-core with electron: A guide

I found this snippet on a different Stack Overflow thread: import electron from "electron"; import puppeteer from "puppeteer-core"; const delay = (ms: number) => new Promise(resolve => { setTimeout(() => { resolve(); }, ms); }) ...

Ways to define a variable based on a CSS class attribute?

I need help figuring out how to create a variable in my script that is defined by the width attribute of a CSS class. The snippet I have currently doesn't seem to work as expected. var dist = $('.nav').css(width()); The goal of my script i ...

Leveraging Angular UI-Router in conjunction with Phonegap

My current project is developed using Angular and I'm deploying it to the Phonegap Build service for iOS and Android distributions. Initially, I used Angular's routing service but had to switch to Angular UI-Router due to nesting multiple views r ...

Tips for utilizing Vue 'component' with just Vue added as a page add-on using <script>:

I am trying to incorporate a Vue component called vue-timeago: import VueTimeago from 'vue-timeago' Vue.use(VueTimeago, { name: 'Timeago', // Component name, `Timeago` by default locale: undefined, // Default locale locales: { ...

Scrolling with JQuery between a webpage's header and footer

I'm having trouble keeping a div at the top of my page, but I want it to stop scrolling before reaching another div. <div class="my-top"><p>This is the top</p></div> <div class="big-one"> <div class="mini1 ...

The variable in Vue.js is failing to update, resulting in a "variable is not defined" error

I've been attempting to display the updated value of the totalQuestions variable in the HTML, but I keep encountering the following error. Can someone point out where I went wrong? https://i.sstatic.net/mEEMS.jpg HTML <label><span class="r ...

Unable to trigger button retrieved through ajax request

I am facing a minor issue with my php page. It returns a button using ajax, as you can see below: HTML snippet: <a href="#" class="unit_register_button"> Register </a> jQuery code: $('a.unit_register_button').click(function(e) ...