[FIXED] Invalid print session

What I’m trying to do: Export my form to PDF
When I try to export my form to PDF, I get an error in my output: “AnvilWrappedError: Invalid print session”. I have tried several things with no luck. What am I doing wrong? Newish programmer. Frist day with Anvil.

What I’ve tried and what’s not working: I have looked at

Code Sample:

# Client
from ._anvil_designer import QueryTemplate
from anvil import *
import anvil.server
import anvil.media

class Query(QueryTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    # Any code you write here will run when the form opens.

  def submit_click(self, **event_args):
    """This method is called when the button is clicked"""
    result = anvil.server.call('get_meter_info', self.meter_id.text)
    print(result[:-1])
    self.result.text = result[-1]

  def export_click(self, **event_args):
    """This method is called when the button is clicked"""
    media_object = anvil.server.call('export_to_pdf', self.meter_id.text, self.result.text)
    anvil.media.download(media_object)

#########

# Server via Uplink
from arcgis import GIS
from arcgis.features import FeatureLayer

import anvil.server
import anvil.pdf
from arcgis.raster.functions import med
from anvil.pdf import PdfRenderer

anvil.server.connect("REMOVED AFTER PASTE")

#TODO: May want to check if user is already logged in
gis = GIS(profile='REMOVED AFTER PASTE')
print(gis.users.me.username)

def get_meter_by_id(miu_id, lyr):
    keep_fields = ['Meter_Serial', 'Status', 'Meter_Installation', 'Meter_Type', 'Meter_Size', 'Manufacturer', 'Asset_ID', 
    'Billing_Account_Num', 'Consumption', 'Meter_Cycle', 'Meter_Route', 'Walk', 'Fund_ID', 'Meter_Number', 'Account_Name', 
    'Account_Number', 'Address', 'MIU_ID', 'Meter_Number1', 'Meter_Size1', 'Reading', 'Last_Reading', 'Meter_Read', 
    'Days_Since_Last_Read', 'Meter_Status', 'Route_Cycle', 'c2_times_Avg_Consumption', 'Days_of_Above_Avg_Cons', 
    'Occupancy_Status', 'Maintenance_Requested', 'Included_in_Analysis']
    #lat_long_sr = 4326
    #state_plane_sr = 3857
    wgs_84_sr = 3857
    return lyr.query(where=f"MIU_ID = {miu_id}", out_fields=keep_fields, out_sr=wgs_84_sr)

@anvil.server.callable
def export_to_pdf(meter_id, result):
    media_obj = anvil.pdf.render_form('Query', meter_id, result)
    return media_obj

@anvil.server.callable
def get_meter_info(meter_id):
    agol_item = gis.content.get('df1e1e2ce742480288318f22e4be721b')
    feat_lyr = FeatureLayer.fromitem(agol_item)
    result = get_meter_by_id(meter_id, feat_lyr)
    return ('logged in', meter_id, feat_lyr.properties.name, result.to_dict()['features'])

Clone link:
share a copy of your app

Hi @austindoezema,

This looks like it might be a bug in Anvil, so I’ve provisionally moved it to Bug Reports.

Can you provide a Clone link (or a link to a running app) to demonstrate this problem, so that we can investigate further?

I am experiencing this as well when creating PDFs. I can’t so easily share a clone link or a link to the app because it is an internal tool behind login credentials.

the error happens on the last line pdf = a.render_form(‘’) . This is called during a background task.

@anvil.server.callable
def add_engagement_letter(claim, attorney):
  
  claimant_name='{} {}'.format(claim['claimant']['first_name'], claim['claimant']['last_name'])
  a = anvil.pdf.PDFRenderer(filename='{} ({}) Engagement Letter.pdf'.format(claimant_name, claim['reference_id']))
  replacements = {'*Date*':datetime.date.today().strftime('%B %d, %Y'),
                  '*Customer Name*':claimant_name,
                  '*Customer Email*':claim['claimant']['email'],
                  '*Customer Phone*':claim['claimant']['phone_number'],
                  '*Attorney Name*':attorney['name'],
                  '*Company Name*':claim['company']['entity_name'],
                  '*Attorney Email*':attorney['email']
                 }
  
  signature=claim['signature']
  pdf = a.render_form('attorney_engagement',signature, replacements)

Has it been solved yet? this topic is categorized as FIXED but i cant see any solution

Hi @joinlook - I’ve been able to reproduce this error, and we’re looking into it! Thanks for reporting it.

Hi all, we think we’ve now (re-)fixed this issue. Please let us know if you continue to see the errors!

1 Like

2 posts were split to a new topic: [BUG] Invalid Print Session

@meredydd @eli
From time to time I get this error as well. The strange thing is that when I try the same output it succeeds. So it happens sometimes and can mostly be overcome by trying again.
Still, it is confusing for users.

Here is a log session (ID: ASHVVE7VWQEGI7SLRWEH2EYQ2FV23M6M):

Starting MainForm
MainForm: before select panel
MainForm: before update
Starting MainForm
MainForm: before select panel
MainForm: before update
Starting MainForm
MainForm: before select panel
MainForm: before update
AnvilWrappedError: Invalid print session
at /downlink/anvil/pdf.py, line 17
called from /downlink/anvil/pdf.py, line 30
called from data, line 38
called from data, line 72
called from MainForm, line 111
called from CheckPanel, line 47
called from CheckPanel, line 47
called from CheckPanel, line 37
called from MainForm, line 78
called from ProgressPanel, line 24
AnvilWrappedError: Invalid print session
at /downlink/anvil/pdf.py, line 17
called from /downlink/anvil/pdf.py, line 30
called from data, line 38
called from data, line 72
called from MainForm, line 111
called from CheckPanel, line 47
called from CheckPanel, line 47
called from CheckPanel, line 37
called from MainForm, line 78
called from ProgressPanel, line 24

I get this error when I try to generate a PDF by Uplink. Is that expected?