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

[suggested sample code] PyMuPDF online web console #3984

Closed
warren-bank opened this issue Oct 23, 2024 · 2 comments
Closed

[suggested sample code] PyMuPDF online web console #3984

warren-bank opened this issue Oct 23, 2024 · 2 comments

Comments

@warren-bank
Copy link

Discover the PyMuPDF online web console includes several blocks of sample code..
to illustrate cool things that can be done from within the console.

The console provides a File Loader button to input a PDF file.
However, the page doesn't show any way to output the PDF file after it has been modified by PyMuPDF.

May I suggest a new sample that shows how to do so?
I'm thinking something along the lines of:

import base64
import js
bytes = doc.tobytes(garbage=3, deflate=True)
data_uri = 'data:application/octet-stream;base64,' + base64.b64encode(bytes).decode('ascii')
div = js.document.createElement('div')
div.innerHTML = '<a href="' + data_uri + '" download="out.pdf">Download modified PDF file</a>'
js.document.body.prepend(div)
@warren-bank
Copy link
Author

here is a more complete/polished example:

        <h2>Scrub a document and add a button to save the result</h2>
        <div id="pythonScript" class="e">
          <pre>
<code>import pyodide.http</code>
<code>r = await pyodide.http.pyfetch('https://raw.githubusercontent.com/bpampuch/pdfmake/0.2.14/examples/pdfs/links.pdf')</code>
<code>data = await r.bytes()</code>
<code>doc = pymupdf.Document(stream=data)</code>
<code>doc.scrub(reset_responses=False)</code>
<code></code>
<code>import base64</code>
<code>bytes = doc.tobytes(garbage=3, deflate=True)</code>
<code>data_uri = 'data:application/octet-stream;base64,' + base64.b64encode(bytes).decode('ascii')</code>
<code></code>
<code>import js</code>
<code>save_button = js.document.getElementById('fileSaveButton')</code>
<code></code>
<code>if not save_button:</code>
<code>    open_button = js.document.getElementById('fileOpenButton')</code>
<code>    if open_button:</code>
<code>        save_button = js.document.createElement('button')</code>
<code>        save_button.setAttribute('id', 'fileSaveButton')</code>
<code>        save_button.style.display = 'inline-block'</code>
<code>        save_button.style.marginLeft = '1em'</code>
<code>        open_button.style.display = 'inline-block'</code>
<code>        open_button.parentNode.appendChild(save_button)</code>
<code></code>
<code>if save_button:</code>
<code>    save_button.innerHTML = '&lt;a href="' + data_uri + '" download="result.pdf"&gt;Save&lt;/a&gt;'</code>
<code>    save_anchor = save_button.childNodes[0]</code>
<code>    save_anchor.style.color = 'inherit'</code>
<code>    save_anchor.style.textDecoration = 'none'</code>
</pre>
          <button onClick="parseScript('e')">RUN &gt;</button>
        </div>

notes:

  • because my <code> blocks include html entities (to prevent the html tags in strings from being added to the DOM),
    I needed to update the parseScript function:
    • replace: commands.push($(this).html());
    • with: commands.push($(this).text());
  • the choice to use innerHTML was made after an initial attempt to build save_anchor using DOM methods
    • Firefox seems to hang when attempting to call: save_anchor.setAttribute('href', data_uri)
    • interestingly, innerHTML has no such issue
  • the choice of PDF file to download and scrub was made pretty quickly.. the main criteria were:
    • in a github repo, since it's unlikely to be deleted
    • small
    • includes several links.. to make sure that links are removed
  • I should mention.. that when I originally tested this script using the same PDF file as the other samples
    • doc.scrub() failed
    • doc.scrub(reset_responses=False) works.. but this parameter is required

@jamie-lemon
Copy link
Collaborator

Thanks for the suggestion, unfortunately the codebase on pymupdf.io is closed source so we are unable to accept code suggestions or 3rd party contributions at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants